Auth: login with username instead of email

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.

Update: Thanks to the readers and their comments – it’s even more simple than I’ve written in the first version of the article.

All we need is add a property in app/Http/AuthController.php:

protected $username = 'username';
Want more articles like this every week? Subscribe!




Still not sure? Want to check out past newsletter issues?
Here they are – just click this link!

The answer lies in framework file:
Illuminate\Foundation\Auth\AuthenticatesUsers.php

    public function postLogin(Request $request)
    {
        $this->validate($request, [
            $this->loginUsername() => 'required', 'password' => 'required',
        ]);

And then let’s look into loginUsername() method.

    public function loginUsername()
    {
        return property_exists($this, 'username') ? $this->username : 'email';
    }

As you can see, it’s looking for a property $this->username, otherwise defaults it to ’email’.
Expecting more steps? Sorry to disappoint, that’s all you need to do. Simple, isn’t it?

Like our articles?
Check out our Laravel online courses!

29 COMMENTS

  1. 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

  2. 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 🙁

  3. 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.

  4. 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

  5. I achieved this in 5.3 by declaring a public function “username” to override the default “return email” to “return username”.

  6. 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?

LEAVE A REPLY

Please enter your comment!
Please enter your name here