我们正在尝试扩展 Devise (3.1.1) 登录/注册方法来处理 AJAX 请求,但被可确认的逻辑卡住了。通常,如果用户在确认他们的帐户之前登录到 Devise,他们将被重定向到登录屏幕,并显示闪烁消息:“您必须在继续之前确认您的帐户。” 我们无法弄清楚 Devise 在哪里检查确认并做出重定向的决定。
这是我们扩展的session_controller代码。它适用于成功和失败的登录尝试:
# CUSTOM (Mix of actual devise controller method and ajax customization from http://natashatherobot.com/devise-rails-sign-in/):
def create
# (NOTE: If user is not confirmed, execution will never get this far...)
respond_to do |format|
format.html do
# Copied from original create method:
self.resource = warden.authenticate!(auth_options)
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location => after_sign_in_path_for(resource)
end
format.js do
# Derived from Natasha AJAX recipe:
self.resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")
sign_in(resource_name, resource)
return render :json => {:success => true, :token => form_authenticity_token() }, content_type: "application/json" # Need to explicitely set content type to JSON, otherwise it gets set as application/javascript and success handler never gets hit.
end
end
end
def failure
return render :json => {:success => false, :errors => ["Login failed."]}
end
问题是,如果用户未经确认,则该create
方法永远不会受到打击。重定向发生在之前的某个地方,这意味着我们无法以 JS 友好的方式处理它。但是通过查看源代码,我找不到任何可以进行可确认检查的过滤器。可确认的检查发生在哪里,我们如何拦截它?