Devise redirect after users fail signin

穿精又带淫゛_ 提交于 2019-12-11 16:28:56

问题


I know there is a wiki page about it, but since I'm very new to Rails I am having lot of difficulties in understanding that page.

I had to override registration controller for my user. When user fails to signin I want him to be redirected to my custom signin page. But the application send him to the signin page inside the gem.

How can I accomplish that? Where should I put this class and how can I change it? I have multiple models, each of them has a different signin page. How can I set the scope for each model?

class CustomFailure < Devise::FailureApp

  def redirect_url

    #return super unless [:worker, :employer, :user].include?(scope) #make it specific to a scope
    new_user_session_url(:subdomain => 'secure')
  end

 # You need to override respond to eliminate recall
 def respond
   if http_auth?
     http_auth
   else
     redirect
   end
 end

end

Devise is a powerful gem but some wiki pages don't consider that there can be lots of new programmers


回答1:


I have my auth failure over-ride class in /lib directly.

Here's a bare-bones version that shows how to handle different scopes for users.

class MyAuthFailure < Devise::FailureApp

  # add different cases here for diff scopes.
  def redirect_url 
    if warden_options[:scope] == :user 
      root_path 
    elsif warden_options[:scope] == :admin 
      admin_root_path 
    end 
  end 

  # You need to override respond to eliminate recall
  def respond
    if http_auth?
      http_auth
    else
      redirect
    end
  end  
end

You'd put this class in /lib/my_auth_failure.rb




回答2:


This solution works great, but the you'll run into problems testing if you don't do what is suggested in this article:

https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated

Your tests will still redirect using the default.

for example:

current_path.should == signin_path

would fail because the current path would actually be users/sign_in or something.



来源:https://stackoverflow.com/questions/11018335/devise-redirect-after-users-fail-signin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!