Skip to content

Instantly share code, notes, and snippets.

@DBA
Created January 4, 2009 01:58
Show Gist options
  • Save DBA/42980 to your computer and use it in GitHub Desktop.
Save DBA/42980 to your computer and use it in GitHub Desktop.
# This custom version of the authenticate method allows the user to use login or email as unique keys.
# Please note that if using this method it's highly recommended that you make the email property, of the user model, a required one.
# Also consider adding a unique index to the email field. For that create a migration with: add_index :users, :email, :unique => true
# It's equally advisable to remove the nullable option from the email field.
def self.authenticate(login, password)
return nil if login.blank? || password.blank?
u = find :first, :conditions => ['(email = ? OR login = ?) and activated_at IS NOT NULL', login, login] # need to get the salt
u && u.authenticated?(password) ? u : nil
end
@benoror
Copy link

benoror commented Oct 28, 2010

And which is the best way to achive that ? I mean, are there other pieces of code involved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment