Devise: find by username OR email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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