Skip to content

Navigation Menu

Sign up

How To: Customize user account status validation when logging in

dayanandprabhu edited this page Dec 6, 2011 · 12 revisions

Sometimes you want to add custom validation to the user before logging them in. In this case I needed to implement a account_active boolean (true or false) check. So if it's true it will allow the user to login and create a session, if false it will display the "account not active" error.

Overwrite the active_for_authentication? method in your model (User) and add your validation logic. You want to return super && (true or false)

 def active_for_authentication?
 # Comment out the below debug statement to view the properties of the returned self model values.
 # logger.debug self.to_yaml
 	
 super && account_active?
 end

In this case it checks the boolean value to account_active and it return it's value to the sign in method that called it.

The original active_for_authentication? method can be found in devise/lib/devise/models/authenticatable.rb.

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /