Auth: login with username instead of email

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

Like our articles?
Check out our Laravel online courses!

33 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?

  7. 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();
    }

  8. 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”

LEAVE A REPLY

Please enter your comment!
Please enter your name here