Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make use of "username or Email", to login in devise?

I found a way to customize devise to use username to login instead of email address.
But actually I want to use combination :-

Enter username or Email:   <text-box>
Enter your password    :   <text-box>  

So how to achieve this ?

Actually we need to modify devise gem's configuration to support this.
for now I know that modifying the "/config/initializers/devise.rb" file as

config.authentication_keys = [ :username ] (edited from email to username.)  

would actually enable logging in using username instead of emailid.
But can I do some thing like:-

config.authentication_keys = [ :username | :email] in the "config/initializers/devise.rb" file.  

So that we devise can take username or email as authentication key at runtime depending on the input provided by user.
Thanks in advance .

like image 980
Hemanth Avatar asked Nov 07 '10 07:11

Hemanth


People also ask

Should I use email or username for login?

Email addresses are useful as users should already remember then and they don't have to think about anything. This is useful if you don't have a "username" on your app. Example: Paypal or Dropbox, where no user name is needed to use these apps and the email is the best way to connect with other people.

What is devise authentication?

Devise is a well known solution for authentication in Rails applications. It's full featured (it not only adds authentication but also password recovery, email changing, session timeout, locking, ip tracking, etc.) and can be expanded to add even more (like JWT authentication).


2 Answers

Look at there : https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address (url updated)

It explains how to allow users to sign in using their username or email address.

like image 164
Arkan Avatar answered Oct 11 '22 06:10

Arkan


how about filtering on the username or email if there is an @ and a . which would be characteristics for an email.

like image 22
steveoh Avatar answered Oct 11 '22 06:10

steveoh