← Impulso Blog

Authentication in Laravel and how to change credential validation

Learn how Laravel’s default authentication works and how to customize credential validation with a user provider.

Person using a laptop in front of monitors displaying a code editor.

Editorial translation by Impulso.

Devs are always looking for extra tips to make their day-to-day work easier. And one of the biggest headaches they run into is changing how credentials are validated in a system. Laravel authentication is a great option, and it isn’t all that hard to set up. With Laravel, you can set up a standard login (with an email address or username and a password) to authenticate users in the system you’re building. Authentication is covered in the documentation under Security > Authentication.

Following the documentation, you set up the login with views, controllers, models, and migrations to create the database table. So far, so good, right? Well, questions usually come up when you already have a database in production and the new system you’re building needs to use it to authenticate users. The password is usually encrypted in that database in a format different from the one Laravel uses. That’s why I’ll show you how to change the way Laravel validates user credentials. That way, you can build your own validation logic and authenticate your users.

Default Laravel authentication

The first step is to set up Laravel’s default authentication as described in this documentation. Next, you need to modify config/auth.php. The process is made easier by using guards and providers.

  • Guards define how users are authenticated for each login request. For example, Laravel comes with a session guard that maintains state using session storage and cookies. So you need to modify config/auth.php, create a custom guard, and specify it in the provider.
  • The provider is the part you’ll extend to override the method that validates credentials.

'guards' => [        'web' => [            'driver' => 'session',            'provider' => 'csauth',        ],

'api' => [            'driver' => 'token',            'provider' => 'users',            'hash' => false,        ],

'customcs' => [            'driver' => 'session',            'provider' => 'csauth',        ]    ],

In the provider, make the following change:

'providers' => [
       'users' => [
           'driver' => 'eloquent',
           'model' => App\User::class,
],

       'csauth' => [
           'driver' => 'customcs',
           'model' => App\User::class,
]

       // 'users' => [
       //     'driver' => 'database',
       //     'table' => 'users',
       // ],
],

Modifying app/Providers/AuthServiceProvider.php

This provider determines which authentication method Laravel uses. If you leave the method boot() unchanged, Laravel will keep using its default method. Here, we’ll use CustomEloquentUserProvider. To do that, add the following to the boot() method:

public function boot()
   {
      $this->registerPolicies();
       Auth::provider('customcs', function ($app, array $config) {
           return $app->make(CustomEloquentUserProvider::class, ['model' => $config['model']]);
});
}

Creating the provider app/Providers/CustomEloquentUserProvider****.php

In this provider, we’ll add our custom credential validation. This lets you implement whatever logic your project requires. If the validateCredentials method returns true, your user is logged in.

php namespace App\\Providers; use Illuminate\\Auth\\EloquentUserProvider; use Illuminate\\Contracts\\Auth\\UserProvider; use Illuminate\\Contracts\\Auth\\Authenticatable as UserContract; class CustomEloquentUserProvider extends EloquentUserProvider {    public function validateCredentials(UserContract $user, array $credentials)    {        $plain = $credentials\['password'\]; if ($plain == $seuModoDeValidacao) {            return true;        } else {            return false;        } // If you want to keep authenticating with Laravel’s default method as well,        // use the line below to validate credentials both ways.        // return parent::validateCredentials($user, $credentials);    } } ## **Now you can validate any credential** To wrap up, I also showed you how to create your own **_UserProvider_**. The advantage of putting the logic in the provider is that you can always use the same algorithm to authenticate users with their credentials, and **it’s easy to share that logic across projects or multiple _guards_.** Every **_UserProvider_** needs a _validateCredentials_ method that checks the password _hash_ against the one found in the database. Keep in mind that you don’t have to create a _provider_ from scratch. You can create one that extends Laravel’s user provider and override just this method. Now it’s time to validate any credentials you need to, or even create a User. For more tips like these, follow our blog. And if you have questions, you can get answers in our [Discord community](http://impulso.link/discord), where we have plenty of expert devs on hand!

Related articles