Devise redirect after login fail

Solution 1:

  1. Create a custom_failure.rb in your lib directory, with:

    class CustomFailure < Devise::FailureApp
      def redirect_url
        your_path
      end
    
      def respond
        if http_auth?
          http_auth
        else
          redirect
        end
      end
    end
    
  2. In you Devise initializer, include:

      config.warden do |manager|
        manager.failure_app = CustomFailure
      end
    
  3. Make sure Rails is loadin your lib files, in your application.rb :

    config.autoload_paths += %W(#{config.root}/lib)
    

Don't forget to restart your server.

I don't think there's an easier way to do this. Good luck.

Solution 2:

If you use your own SessionsController, you can re-assign the :recall value of auth_options to recall the controller#method you want before running warden.authenticate!(auth_options), for example:

in app/controllers/users/sessions_controller.rb

class Users::SessionsController < Devise::SessionsController
  #...
  def create
    #...
    auth_options = { :recall => 'site#index', :scope => :user }
    resource = warden.authenticate!(auth_options)
    #...
  end
  #...
end

With this way, you don't need to create the customized FailureApp and modify the configs.