I've multiple guards in my laravel application:
Code config/auth.php
:
'defaults' => [
'guard' => 'user',
'passwords' => 'users',
],
'guards' => [
'user' => [
'driver' => 'token',
'provider' => 'users',
'hash' => true,
],
'admin' => [
'driver' => 'token',
'provider' => 'admins',
'hash' => true,
]
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => Admin::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'users_password_resets',
'expire' => 60,
'throttle' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'admins_password_resets',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
And has route for get authenticated user in api.php
:
Route::get('admins/auth/user', 'AuthController@user')->middleware('auth:sanctum');
Also in my models (Admin, User) used trait:
Laravel\Sanctum\HasApiTokens
When I tried get auth user by token get error with message:
InvalidArgumentException: Auth guard [web] is not defined. in file appname\vendor\laravel\framework\src\Illuminate\Auth\AuthManager.php on line 84
#0 appname\vendor\laravel\framework\src\Illuminate\Auth\AuthManager.php(68): Illuminate\Auth\AuthManager->resolve()
#1 appname\vendor\laravel\sanctum\src\Guard.php(45): Illuminate\Auth\AuthManager->guard()
#2 [internal function]: Laravel\Sanctum\Guard->__invoke()
#3 appname\vendor\laravel\framework\src\Illuminate\Auth\RequestGuard.php(58): call_user_func()
#4 appname\vendor\laravel\framework\src\Illuminate\Auth\GuardHelpers.php(60): Illuminate\Auth\RequestGuard->user()
#5 appname\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php(63): Illuminate\Auth\RequestGuard->check()
#6 appname\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php(42): Illuminate\Auth\Middleware\Authenticate->authenticate()
I've runned already commands:
php artisan cache:clear
php artisan config:cache
And also tried add guard name to config/sanctum.php
:
'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', 'localhost,127.0.0.1')),
'expiration' => null,
'middleware' => [
'verify_csrf_token' => App\Http\Middleware\VerifyCsrfToken::class,
'encrypt_cookies' => App\Http\Middleware\EncryptCookies::class,
],
// This line added
'guard' => 'admin'