Instantly share code, notes, and snippets.

Embed
What would you like to do?
Devise: find by username OR email
class User
extend ClassMethods
devise :database_authenticatable, :omniauthable, :registerable, :confirmable, :recoverable, :rememberable
module ClassMethods
def find_for_database_authentication(conditions)
value = conditions[authentication_keys.first]
where(["username = :value OR email = :value", { :value => value }]).first
end
def send_reset_password_instructions(attributes={})
super(authentication_attribute(attributes))
end
def send_confirmation_instructions(attributes={})
super(authentication_attribute(attributes))
end
protected
# determine email by username
def authentication_attribute(attributes)
user = self.find_by_username(attributes[:email])
attributes[:email] = user[:email] if user
attributes
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment