diff --git a/README.md b/README.md index 9c39f00..14a219e 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ bundle exec sidekiq -C config/sidekiq.yml * user[password] * user[password_confirmation] - 返回结果 - + ```json { "user": { @@ -64,7 +64,7 @@ bundle exec sidekiq -C config/sidekiq.yml * user[email] * user[password] - 返回结果: - + ```json { "user": { @@ -78,12 +78,12 @@ bundle exec sidekiq -C config/sidekiq.yml 3 提交分享资源 POST: http://localhost:3000/api/v1/posts - 参数 - * 需要登录 传Authorization header + * 需要登录 传Authorization Bearer header * url 网址 * type article 或者 page * description 可选 - - 返回 - + - 返回 + ``` json { "id": 32, @@ -141,13 +141,13 @@ bundle exec sidekiq -C config/sidekiq.yml ] ``` -5 关注follow 用户:POST http://localhost:3000/api/v1/users/2/follow +5 关注follow 用户:POST http://localhost:3000/api/v1/users/2/follow 6 取消关注unfollow 用户: POST: http://localhost:3000/api/v1/users/2/unfollow 7 喜欢 post POST: http://localhost:3000/api/v1/posts/3/like - - 返回结果 + - 返回结果 ``` json { @@ -173,7 +173,7 @@ bundle exec sidekiq -C config/sidekiq.yml 10 评论列表 GET http://localhost:3000/api/v1/posts/1/comments -- 返回结果: +- 返回结果: ``` json [ @@ -209,7 +209,7 @@ bundle exec sidekiq -C config/sidekiq.yml } ``` -12 取消喜欢评论 comment: POST http://localhost:3000/api/v1/posts/1/comments/1/unlike +12 取消喜欢评论 comment: POST http://localhost:3000/api/v1/posts/1/comments/1/unlike 13 增加文章的浏览数(当用户点击查看某个文章时使用),不需要关心返回值 POST http://localhost:3000/api/v1/posts/3/view @@ -250,14 +250,14 @@ bundle exec sidekiq -C config/sidekiq.yml } ] ``` - -15 获取最受欢迎的文章 GET http://localhost:3000/api/v1/top - - 参数 - * 无 - - - 返回结果 - +15 获取最受欢迎的文章 GET http://localhost:3000/api/v1/top + + - 参数 + * 无 + + - 返回结果 + ```json [ { @@ -278,10 +278,10 @@ bundle exec sidekiq -C config/sidekiq.yml "good_format": false, "created_at": "2015-08-07T00:25:56.942+08:00" } - ] - ``` - + ] + ``` + #### 注意 - * 注意,API返回结果中的token是利用JWT 技术加密的token,密钥是每个用户的email + * 注意,API返回结果中的token是利用JWT 技术加密的token * [jwt](http://jwt.io/) * 通常执行创建(POST) 操作时,如果没有返回结果,仅仅返回200,也是成功的表示, 当返回400时即为失败 diff --git a/app/controllers/api/v1/application_controller.rb b/app/controllers/api/v1/application_controller.rb index 23cae08..bc3b92e 100644 --- a/app/controllers/api/v1/application_controller.rb +++ b/app/controllers/api/v1/application_controller.rb @@ -19,18 +19,22 @@ class Api::V1::ApplicationController < ActionController::Base id: user.id, name: user.name, email: user.email, - token: JWT.encode({token: user.active_auth_token}, user.email) + token: JWT.encode({id: user.id}, 'tuxue', 'HS256') } } end def authenticate_user_from_token - auth_token = AuthToken.find_by(value: request.headers["Authorization"].try(:split, ' ').try(:last)) - if auth_token - @current_user = auth_token.user - else + + token = request.headers["Authorization"].try(:split, ' ').try(:last) + begin + decoded_token = JWT.decode token, 'tuxue', true + @current_user = User.find(decoded_token[0]['id']) + return render json: {message: 'Token is not valid'}, status: 401 unless @current_user + rescue JWT::DecodeError => e return render json: {message: 'Token is not valid'}, status: 401 end + end def fetch_current_user diff --git a/app/controllers/api/v1/sessions_controller.rb b/app/controllers/api/v1/sessions_controller.rb index 93ebf8e..3908f70 100644 --- a/app/controllers/api/v1/sessions_controller.rb +++ b/app/controllers/api/v1/sessions_controller.rb @@ -9,6 +9,16 @@ class Api::V1::SessionsController < Devise::SessionsController render json: user_json end + def user_json(user= current_user) + { + user: { + id: user.id, + name: user.name, + email: user.email, + token: JWT.encode({id: user.id}, 'tuxue', 'HS256') + } + } + end def destroy sign_out(resource_name) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 90c13b8..6920295 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -9,23 +9,5 @@ class ApplicationController < ActionController::Base @current_user = current_user if user_signed_in? end - def user_json(user= current_user) - { - user: { - id: user.id, - name: user.name, - email: user.email, - token: JWT.encode({token: user.active_auth_token}, user.email) - } - } - end - def authenticate_user_from_token - auth_token = AuthToken.find_by(value: request.headers["Authorization"].try(:split, ' ').try(:last)) - if auth_token - @current_user = auth_token.user - else - return render json: {message: 'Token is not valid'}, status: 401 - end - end end diff --git a/config/database.yml b/config/database.yml index 83baef1..d1ae2d0 100644 --- a/config/database.yml +++ b/config/database.yml @@ -15,6 +15,7 @@ # gem 'pg' # default: &default + reconnect: true adapter: postgresql encoding: unicode host: postgres