RoR Devise: Sign in with username OR email

前端 未结 11 2715
渐次进展
渐次进展 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:32

    I have found a solution for the problem. I'm not quite satisfied with it (I'd rather have a way to specify this in the initializer), but it works for now. In the user model I added the following method:

    def self.find_for_database_authentication(conditions={})
      find_by(username: conditions[:email]) || find_by(email: conditions[:email])
    end
    

    As @sguha and @Chetan have pointed out, another great resource is available on the official devise wiki.

提交回复
热议问题