Home > Enterprise >  Laravel Passport: how to customize the authentication guard for the user provider in your applicatio
Laravel Passport: how to customize the authentication guard for the user provider in your applicatio

Time:10-09

Laravel by default creates the model and the User table. The problem is that I create and use the table in the database, a model and a controller called Citizen. And here comes my doubt, when we configure laravel-passport we must specify the guard, this is the Laravel example:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
],

What is the provider? Taking into account this line:

        'provider' => 'users',

Should I use 'provider' => 'citizen', instead of 'provider' => 'users'?

CodePudding user response:

You can create custom provider. This has to be done in config/auth.php There is one provider named users by default. You can create your own guard and providers as follows:

guards:

    'guards' => [
    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
    'citizens' => [
        'driver' => 'passport',
        'provider' => 'citizens',
    ],
],

In the providers section:

   'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User::class,
    ],
    'citizens' => [
        'driver' => 'eloquent',
        'model' => App\Models\Citizen::class, //path to your model
    ],
],

Now you can use the middleware with custom guard by using auth:citizens as middleware. For example, in route file,

Route::middleware('auth:citizens')->group(function(){

});
  • Related