【问题标题】:Custom devise sessions controller is not redirecting on error (:recall => not happening)自定义设计会话控制器未在错误时重定向(:recall => 未发生)
【发布时间】:2012-09-15 19:18:40
【问题描述】:

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

我做错了什么?

【问题讨论】:

  • 您找到解决方案了吗?我遇到了同样的问题。
  • 找到任何解决方案了吗?
  • 您是否为#no 操作定义了路线?

标签: ruby-on-rails devise controller


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多