Notice: this article was updated in February 2020 to match the latest Laravel 6 version.
Laravel comes with out-of-the-box authorization mechanism which is incredibly easy to use. But it depends on several pre-defined things, one of the main ones – DB table users structure and login with email field. What if you want to have username to identify a user?
It’s quite simple to change. First, of course, you need to create database migration to add a username column to your DB table, and now let’s see how to use it.
All we need is add a method in app/Http/Controllers/Auth/LoginController.php:
/** * Get the login username to be used by the controller. * * @return string */ public function username() { return 'username'; }
The answer lies in framework file:
Illuminate\Foundation\Auth\AuthenticatesUsers.php
/** * Get the needed authorization credentials from the request. * * @param \Illuminate\Http\Request $request * @return array */ protected function credentials(Request $request) { return $request->only($this->username(), 'password'); }
And then let’s look into username() method.
/** * Get the login username to be used by the controller. * * @return string */ public function username() { return 'email'; }
As you can see, it just defaults to ’email’, unless you override it in Controller.
Expecting more steps? Sorry to disappoint, that’s all you need to do. Simple, isn’t it?
You can read more about Auth customization in this article: 8 Things You Can Customize in Laravel Registration
Nice little tip that I could have used when first learning Auth with Laravel. Thanks!
i think you can use:
protected $username = ‘username’;
in your AuthController.php
Oh cool, didn’t know that. Will update the article.
Great article 🙂 However, in Laravel 5.2, you just need to create protected property called “username” and set it to the desired column in your users table that you wish to use instead of “email” column for logging in. I’ve made a video tutorial on this subject and I hope you will allow this comment, even though it has an external link to the video in it 🙂 Here it is : https://www.youtube.com/watch?v=jAJZO3LurOU
Oh great, didn’t know about 5.2 version, and great video explanation! That’s the power of a blog – good feeling to learn from the readers 🙂
Sharing is caring 🙂
Thank you very much for the video. It is very helpful!
I’m using 5.1. How do you do the same thing but using the DB driver instead? We have an existing database I must use. Thanks.
If I want to do an authentication like Twitter, when the user can typing his email or username, what I have to change ? I think it’s in the AuthenticatesUsers file, but I can not do it 🙁
Hi Aron,
https://laracasts.com/discuss/channels/general-discussion/log-in-with-username-or-email-in-laravel-5
Do you have idea how to use username with JWT Tymon?
Thanks
Got it, just change array of credential into username
https://github.com/tymondesigns/jwt-auth/issues/540
And any way to make case sensitive the login? Thanks
First of all I was read your post 2 months ago, and then after “composer update” the authentication was not working in my site, you have to remember this variable every time when the composer is updated, because maybe foundation/auth will change its code.
Thanks Povilas.
nice tip! thank you!
protected $username = ‘username’;
Why it is not working
it will show error column ’email’ ‘ not found
or if i edit the validation, column username will be not detected so it will just save name and password
it works now
i forget to change name attribute in username input of login.blade.php to ‘username’
thank you
how about forgot password and reset password with username instead of email
I achieved this in 5.3 by declaring a public function “username” to override the default “return email” to “return username”.
God bless you. i was going to edit the AuthenticateUsers function. Thank you so much!
Please, please, please do not change core framework files vendor folder, all the changes will be lost when composer updates your framework files. This is reckless advice any professionally developer wouldn’t be doing this so why are you publishing articles encouraging others to do it?
There is no such file as app/AuthController.php in Laravel 5.4
i have same issue so what i did is following
go to App\Http\Controllers\Auth
you will find LoginController
add this method
public function username()
{
return “name”;
}
this will overwrite the username() method in the trait AuthenticatesUsers
my way to let the user login as username or the name field only.
Thanks a lot, that worked for me. What I’m wondering: How did you find this? Only by reading the source or is there some good documentation to read?
Docs: https://laravel.com/docs/5.4/authentication#username-customization
Thanks for the solution.
Here is another solution if user want to login with username or email
http://www.expertphp.in/article/login-with-username-or-email-in-laravel-5
Thanks for the tutorial but it does not worked for me. This one works:
https://www.youtube.com/watch?v=Qtiyo2J_-tA
I’m using:
Php 7.0.10
MySQL 5.7.14
Apache 2.4.23
Laravel 5
i really dont know how to change email to username could u please help me ,,,
I need to know how to do it for passport, let assume I’ve a login form with username, email and password. If an user provide email and password then make username optional, I mean it should not through an exception. If an user provide username and password then email should be optional. Right now when if username is empty it giving me exception. Please help me to solve the problem.
I’ve this piece of code in my User model.
public function findForPassport($username) {
if (strpos($username, ‘@’) !== false) {
return $this->where(’email’, $username)->first();
}
return $this->where(‘username’, $username)->first();
}
Thank you for this very helpful just one code will override ..
If you would like to include BOTH email AND username as an either/or login option then you can use this replacement for the username function()
“`
public function username()
{
$request = request();
$type = filter_var($request->username, FILTER_VALIDATE_EMAIL) ? ’email’ : ‘username’;
$request[$type] = $request->username;
return $type;
}
“`
don’t forget to change your login.blade form from input name “email” to “username”
thanks This help me !!!