Devise redirect after users fail signin
-
14-06-2021 - |
Question
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
Solution
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
OTHER TIPS
This solution works great, but the you'll run into problems testing if you don't do what is suggested in this article:
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.