Changing Laravel default email field of LoginController

C

Laravel makes implementing authentication very simple. Almost everything is configured out of the box.
Just run below commands in  a fresh Laravel application

php artisan make:auth
php artisan migrate

These two commands will take care of entire authentication system! Login, registration, remember me, forget password all are available.

By default login takes email and password fields as authentication parameter but sometimes we need to modify these like using username instead of email.

If we carefully review the LoginController then we can see it uses AuthenticatesUsers trait which just encapsulates logic. We can override every function of this trait by simply writing the function in the LoginController.

Say we want to use id instead of email. Then copy the username function to LoginController and change return to id.

public function username()
{
    return 'id';
}

We also have to change the login form email input field to id input field and that’s all.

<div class="form-group">
    <label class="col-md-4 control-label">ID</label>

    <div class="col-md-6">
        <input type="text"
               class="form-control"
               name="id"
               value="{{ old('id') }}">
    </div>
</div>

About the author

Imran Pollob

Learn => Practice => Implement => Repeat

Add comment

Categories

Imran Pollob

Learn => Practice => Implement => Repeat

Get in touch