Laravel User Authentication with Username instead of Email

The Laravel standard authentication method is to enter email-address and password to login after the authentication is added with php artisan make:auth.
If you are working on an application for a company that is only used by employees, they have their usernames and passwords, so it doesn’t make sense to sign in with the email-address.

At the registration process, which will be done in this case by an administrator, a username is already requested. I saw some videos for this type of registration with an extra username database field for registration. I’m the existing one.

Laravel 5.4 needs php7. Not all servers of the world run still on php7. So I show how the authentication with the username works on Laravel 5.4 and 5.2.45 which runs under php5.

Laravel 5.4
Edit app/Http/Controllers/Auth/LoginController.php

(click to enlarge)


protected $username = 'name';
 public function username()
 return 'name';

like it is shown i the picture.
Laravel has still a function for authentication with the username. I this case the variable $username is filled with our database field „name“ which is the name of the user which was used at registration.

Edit app/Http/Controllers/Auth/RegisterController.php

(click to enlarge)

Add the „unique“ attribute to „name“ in the Validator.

Edit resources/views/auth/login.blade.php

(click to enlarge)

Change all the the „email“ to „name“. If you use find / replace take care of the type-attribute at input. It should be „text“ and not „name“. 😉

Laravel 5.2.45

Here are other controllers for authentication.

Edit app/Http/Controllers/Auth/AuthController.php

(click to enlarge)

Add only

protected $username = 'name';

like it is shown in the picture and add the „unique“ attribute to „name“ in the Validator.
Than change the view like in Laravel 5.4.