Jump to content

The ultimate community for Ruby on Rails developers.


Photo

[Trick] Devise Gem : custom redirect for failed login

devise login

  • Please log in to reply
No replies to this topic

#1 Kevin

Kevin

    Signalman

  • Members
  • 21 posts

Posted 21 November 2013 - 08:36 AM

If you're failed to login , Devise gem will redirect you to /users/sign_in (back to login page) by default. But there's a high probability you need to adjust it. How to do that ?

 

Create a custom_redirection.rb file under lib folder, and fill that file with this :
class CustomRedirection < Devise::FailureApp
   def redirect_url
     #Your custom redirect path goes here, do whatever you need here
   end
   def respond
     if http_auth?
       http_auth
     else
       redirect
     end
   end
 end

And then, add the following codes to your devise.rb , originally some of them were commented out. Just enable and custom it like this one

 config.warden do |manager|
  #   manager.intercept_401 = false
  #   manager.default_strategies(:scope => :user).unshift :some_external_strategy
    manager.failure_app = CustomRedirection
  end

At last , make sure you have this line : config.autoload_paths = %W(#{config.root}/lib) on your application.rb, restart your web server and try to make a failed login.

 

Taken from my blog at : http://www.kevinhq.c...iled-login.html

 

Do let me know if that trick doesn't work for you. I only tested it on Devise 3.1 with Rails 4 and Ruby 2.0 (rvm)

 







Also tagged with one or more of these keywords: devise, login

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users