obo*_*obo 5 authentication api ruby-on-rails devise ruby-on-rails-3
我正在尝试使用Devise和authenticatable_token构建一个API来登录我的rails应用程序.我在我的API模块中创建了一个SessionsController,并将以下代码写入sign_in:
module Api
module V1
class SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(scope: resource_name, recall: "#{controller_path}#new")
sign_in(resource_name, resource)
if current_user
if !current_user.authentication_token
current_user.reset_authentication_token!
end
render json: {success: true, auth_token: resource.authentication_token, email: resource.email}
else
invalid_login_attempt
end
end
protected
def invalid_login_attempt
warden.custom_failure!
render json: {success: false, message: "Error with your login or password"}, status: 401
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
问题是当我使用没有任何令牌的登录时,应用程序会引发401.
RestClient.post "http://localhost:3000/api/v1/users/sign_in", {user: {email: "tester@test.biz", password: "FILTERED"}}.to_json, :content_type => :json, :accept => :json
Run Code Online (Sandbox Code Playgroud)
但是,如果我设置由注册方法提供的令牌,它可以工作:
response_json = RestClient.post "http://localhost:3000/api/v1/users/sign_in?auth_token=#{@token}", {user: {email: "tester@test.biz", password: "FILTERED"}}.to_json, :content_type => :json, :accept => :json
Run Code Online (Sandbox Code Playgroud)
那么为什么呢?是否可以在执行登录时禁用令牌身份验证过程?
当您使用 json 格式时,Devise 控制器无法开箱即用,您必须执行以下操作之一:
您可以在那里获得一个工作示例http://jessehowarth.com/devise
| 归档时间: |
|
| 查看次数: |
5482 次 |
| 最近记录: |