Laravel - Authentication - Adding Custom Guards
You may define your own authentication guards using the extend
method on the Auth
facade. You should place your call to the extend
method within a service provider. Since Laravel already ships with an AuthServiceProvider
, we can place the code in that provider:
registerPolicies();
Auth::extend('jwt', function ($app, $name, array $config) {
// Return an instance of Illuminate\Contracts\Auth\Guard...
return new JwtGuard(Auth::createUserProvider($config['provider']));
});
}
}
As you can see in the example above, the callback passed to the extend
method should return an implementation of Illuminate\Contracts\Auth\Guard
. This interface contains a few methods you will need to implement to define a custom guard. Once your custom guard has been defined, you may reference the guard in the guards
configuration of your auth.php
configuration file:
'guards' => [
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],