自定义设计会话控制器在出错时未重定向(:recall =>未发生)



我正在覆盖设备会话控制器。。。它可以工作(我可以用json登录),但我不能让它重定向到另一个错误操作:

class Users::SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#no")
return invalid_login_attempt unless resource
sign_in(resource_name, resource)
#respond_with resource, :location => redirect_location(resource_name, resource)
respond_to do |format|
format.json { render :status => 200, :json => { :success => true, :auth_token => resource.authentication_token, :user => resource } }
end
end
def no
puts "NO!"
return render:json => {:success => false, :errors => alert}
end

end

我也试过:

if resource.nil?
puts "NIL!"
end

我总是收到相同的默认Devise json错误,以响应错误的sign_in凭据:

{error: "You need to sign in or sign up before continuing."}

我做错了什么?

请记住,如果管理员没有从中获取用户,则会进行身份验证!动作它向失败的应用程序抛出一个符号。因此,在发生故障的情况下,该行之后的任何内容都是无关紧要的。

authenticate!更改为authenticate,它将返回用户对象供您根据需要进行处理。

查看warden gem中的proxy.rb文件以了解更多详细信息。文档

根据这个类似的线程(扩展Devise SessionsController以使用JSON进行身份验证),您所需要做的就是包含以下代码:

# config/initializers/devise.rb
config.navigational_formats = [:"*/*", "*/*", :html, :json]

最新更新