Devise with Confirmable - Redirect user to a custom page when users tries to sign in with an unconfirmed email

前端 未结 3 1160
自闭症患者
自闭症患者 2020-12-09 05:01

With the Confirmable module enabled, Devise will not allow an unconfirmed user to sign in after a predefined period of time has elapsed. Instead the user is redirected back

3条回答
  •  粉色の甜心
    2020-12-09 05:16

    Sorry at first I thought you meant after Sign Up not Sign In. So the down below works for how to direct users after Sign Up and what you need to do for Sign In is to create a custom Devise::FailureApp

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

    Then within your custom FailureApp overwrite redirect_url method from https://github.com/plataformatec/devise/blob/master/lib/devise/failure_app.rb:

      def redirect_url
        if warden_message == :unconfirmed
          custom_redirect_path
        else
          super
        end
      end
    

    For custom redirect after Sign Up:

    There is a controller method after_inactive_sign_up_path_for within the RegistrationsController that you can overwrite to accomplish this.

    First in your Routes you will need to specify to use your custom controller:

    config/routes.rb:

      devise_for :users, :controllers => { :registrations => "users/registrations" }
    

    Second you create your custom controller that inherits from the normal controller in order to overwrite the method:

    app/controllers/users/registrations_controller.rb

    class Users::RegistrationsController < Devise::RegistrationsController
    
      protected
    
      def after_inactive_sign_up_path_for(resource)
        signed_up_path
      end
    
    end
    

    In this case for my App my Devise model is User so you may want to change that namespace if your model is named differently. I wanted my users to be redirected to the signed_up_path, but you can change that to your desired path.

提交回复
热议问题