Check if user is active before allowing user to sign in with devise (rails)

前端 未结 2 2154
挽巷
挽巷 2020-12-24 12:43

I am using devise and created a User field called :active which is either true or false. I have to manually make the user active (true) before the user is allowed to log in

相关标签:
2条回答
  • 2020-12-24 13:21

    Devise (If you have devise 3.2+) now support block parameter in (session) create

    # assuming this is your session controller
    
    class SessionsController < Devise::SessionsController
    
    def create
      super do |resource|
         unless resource.active?
          sign_out
          # you can set flash message as well.
          redirect_to :sorry_not_active_url
          return
        end
      end
    end
    
    0 讨论(0)
  • 2020-12-24 13:25

    Add these two methods to your user model, devise should pick them up automatically - you should NOT need to extend Devise::SessionsController

    def active_for_authentication?
      super && self.your_method_for_checking_active # i.e. super && self.is_active
    end
    
    def inactive_message
      "Sorry, this account has been deactivated."
    end
    
    0 讨论(0)
提交回复
热议问题