How to change email to username Laravel Login?

Laravel authentication is one vital component supported by the Laravel framework that enables us to easily incorporate user authentication in Laravel applications. The authentication depends on the users table structure in the database that uses email for user identity. We can change a user’s authentication from email to username for Laravel login. There are namely two main ways to do this.

Methods to change email to username Laravel Login

Laravel Breeze

If you are using Laravel Breeze for user authentication, you may find the authentication details in the App/Http/Requests/Auth/LoginRequest.php class. Here you can simply change the authenticate method. Consider the code snippet below.

public function authenticate()
{
    $this->ensureIsNotRateLimited();
 
//(! Auth::attempt($this->only('email', 'password'), $this->boolean('remember')))
    if (! Auth::attempt($this->only('username', 'password'), $this->boolean('remember'))) {
        RateLimiter::hit($this->throttleKey());
 
        throw ValidationException::withMessages([
            'email' => trans('auth.failed'),
        ]);
    }
 
    RateLimiter::clear($this->throttleKey());
}

The comment above the if condition in the code snippet above shows that the condition had previously been taking email and password as auth params, you can simply change them to username as shown above.

Laravel Jetstream

Jetstream uses Laravel Fortify for handling authentication. To save additional data, we will need to customize users authentication. Typically this should be done in the JetstreamServiceProvider, in the boot() method.

Laravel Jetstream uses Fortify to handle user auth. Here, we can customize the user auth function to save any additional data. You can edit the boot method in the JetstreamServiceProvider. Consider the code snippet below.

public function boot()
{
    Fortify::authenticateUsing(function (Request $request) {
//      $user = User::where('email', $request->email)->first();

        $user = User::where('username', $request->username)->first();
 
        if ($user &&
            Hash::check($request->password, $user->password)) {
            return $user;
        }
    });
}

You just need to check the username field instead of email in the boot method to authenticate the user.

Laravel UI Change

To display the username prompt to a user, you will have to alter the Laravel UI. Open the resources/views/auth/login.blade.php file for this and change the email field to username in the fowm. Consider the code snippet below.

<!-- Change 'email' to 'username' in the input field -->
<div>
    <label for="username">Username</label>
    <input id="username" type="text" name="username" value="{{ old('username') }}" required autofocus>
</div>

resources/views/auth/login.blade.php file to use the username field instead of email. Update the form to use the username input field.

Laravel Database Change

Similarly, you will have to add a column in your applications database for the username field. Open the App\Models\User.php for that and add the ‘username’ field in the fillable variable. Run the php run migrate command after this to reflect the database changes.