Current in my warden manager we have a custom failure app declared.

    Rails.configuration.middleware.use Warden::Manager do |manager|
      manager.default_strategies :password
      manager.failure_app = lambda { |env| SessionsController.action(:new).call(env) }
    end

However, for one of my strategies I want to use a different failure action so I tried declaring a custom action in the failure as such:

 def authenticate!
   email =  params["email"] || params['session']['email']
   pw =  params["password"] || params['session']['password']
   user = User.find_by_email email
   if user && user.authenticate_and_activated(pw)
     success! user
   else
     throw(:warden, :stuff => "foo", :action => :failure)   
   end
 end

and even in my controller action:

 def sign_in
   @user = warden.authenticate! :action => :failure
   ...
 end

and here is my failure action:

def failure        
  warden.custom_failure!
  render :json => {:success => false, :errors => ["Login Failed"]}
 end

But the issue is that it seems to be ignoring it and only calling the action declared in my manager. Anyone have an idea of what I could be doing wrong?

有帮助吗?

解决方案

I was able to figure out my solution with an idea suggested by JonRowe on the Warden issues on Github(https://github.com/hassox/warden/issues/73)

With his suggestion of altering my lambda, I was able to change my warden manager configuration to be:

Rails.configuration.middleware.use Warden::Manager do |manager|
  manager.default_strategies :password
  manager.failure_app = lambda { |env| 
    failure_action = env["warden.options"][:action].to_sym
    SessionsController.action(failure_action).call(env) 
  }
end

This picked up the failure action I was passing into it. It seems it is stored in the "warden.options" hash under action.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top