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

前端 未结 3 1172
自闭症患者
自闭症患者 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:19

    I just did this, but took a different approach.

    in app/controllers/sessions_controller.rb:

    class SessionsController < Devise::SessionsController
    
      before_filter :check_user_confirmation, only: :create
    
      #
      # other code here not relevant to the example
      #
    
    private
    
      def check_user_confirmation
        user = User.find_by_email(params[:email])
        redirect_to new_confirmation_path(:user) unless user && user.confirmed?
      end
    end
    

    This worked for me and seemed minimally invasive. In my app new sessions always have to go through sessions#create and users always sign in with their email address, so this may be a simpler case than yours.

    You can of course redirect_to any location you desire in the check_user_confirmation method. new_confirmation_path was the logical choice for me because it provides users with the resources to get confirmed.

提交回复
热议问题