我目前正在使用 Throughbot 的 Clearance 进行身份验证。我需要将 API 添加到我的产品中,但似乎找不到有关使用 Clearance 和 API 的文档。是否有某个标题我可以设置清除将自动检查,如果没有,我可以使用什么?我想我也许可以使用这个。
问问题
1225 次
1 回答
2
为了解决这个问题,我最终覆盖了模型和模型authenticate
上的方法。它看起来像这样:ApplicationController
User
class ApplicationController < ActionController::Base
include Clearance::Controller
include Clearance::Authentication
def authenticate(params)
if request.headers['AUTH-TOKEN']
return nil unless user = User.where(remember_token: request.headers['AUTH-TOKEN']).first
sign_in user
else
User.authenticate(params[:session][:email], params[:session][:password])
end
end
#rest of class omitted for bevity
end
然后我子类SessionsController
来覆盖这样的create
方法:
class SessionsController < Clearance::SessionsController
def create
@user = authenticate(params)
sign_in(@user) do |status|
respond_to do |format|
if status.success?
format.html { redirect_back_or url_after_create }
format.json { render json: @user, status: :ok }
else
format.html do
flash.now.notice = status.failure_message
render template: 'sessions/new', status: :unauthorized
end
format.json { render json: [errors: status.failure_message], status: :unauthorized }
end
end
end
end
#rest of class omitted for bevity
end
然后,您所要做的就是测试或使用将请求AUTH-TOKEN
标头设置为users
记住令牌,然后一切就绪。我选择使用记住令牌,因为它会在用户注销时更新。您可能不希望这种情况发生,而是可以auth_token
在模型上生成一个字段并更改where
为使用新字段。
于 2014-10-08T01:17:38.783 回答