Getting Devise to accept a username or email at login
Devise has quickly become the de facto Rails authorization tool. I'm working with it for the first time and have been impressed with everything it can do. It's very complete, a little too much for my taste, but it plays nicely with Rails 3. I had to dig around in order to accept either a username or email address at login, since by default it only accepts one. If you modify the config.authorization_keys to include 2 paramaters, it requires both at login and is really intended for a custom subdomain. Multiple people were asking the same question and nobody presented a solution, so here is mine. I hope it saves people some time. Set config.authorization_keys to :login.



Comments 2 Comments
conditions[:email] = conditions.delete(:login)
Nice use of the super keyword.
conditions = ["username = ? or email = ?", conditions[authentication_keys.first], conditions[authentication_keys.first]]
# raise StandardError, conditions.inspect
super
end