RoR Devise: Sign in with username OR email

前端 未结 11 2730
渐次进展
渐次进展 2020-12-07 10:15

What\'s the best way to enable users to log in with their email address OR their username? I am using warden + devise for authentication. I think it probably won\'t be too h

11条回答
  •  南方客
    南方客 (楼主)
    2020-12-07 10:31

    I wrote like this and it works out. Don't know if it's "ugly fix", but if I'll come up with a a better solution I'll let you know...

     def self.authenticate(email, password)
       user = find_by_email(email) ||
         username = find_by_username(email)
       if user && user.password_hash = BCrypt::Engine.hash_secret(password, user.password_salt)
         user
       else
         nil
       end
    end
    

提交回复
热议问题