4

我正在覆盖设备会话控制器......它可以工作(我可以使用 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

响应错误的登录凭据,我总是得到相同的默认设计 json 错误:

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

我究竟做错了什么?

4

2 回答 2

2

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

更改authenticate!authenticate,它将返回用户对象以供您按照您认为合适的方式处理。

检查warden gem 中的proxy.rb 文件以获取更多详细信息。 文档

于 2013-04-10T12:53:34.267 回答
1

根据这个类似的线程(Extending Devise SessionsController to authenticate using JSON),您需要做的就是包含以下代码:

# config/initializers/devise.rb
config.navigational_formats = [:"*/*", "*/*", :html, :json]
于 2014-03-12T22:13:23.353 回答