问题
I'm testing with laravel sanctum but here some issues.. I'm creating Admin guard.
When I change the middleware to auth:sanctum_admin.. it should be only can access by admin but here I can access with normal user account with web guard. I don't know why?...I used passport with multiauth package.it's fine. but here in sanctum can't be separate User Table and Admin.
回答1:
You can, also use multiple guards in sanctum. To achieve this, follow these steps -
- Create your own guard as required. (In
config/auth.php
)
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
]
],
- Set providers. (In
config/auth.php
)
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
],
- Use this guard when you authenticate a user. (In route file)
if(auth()->guard('admin')->attempt($request->only('email','password'))) { return auth()->guard('admin')->user(); }
回答2:
@Abhishek Mitra
and for authorizatioin using Laravel Sanctum in case of Multiple Auth Guard
, we can use middleware as such
Route::middleware(['auth:guard_name'])->get('/user', function(){
return auth()->guard('guard_name')->user();
}
回答3:
I think the default guard should be like this:
'defaults'{
'guard' : "sanctum_admin",
'passwords': 'admins',
}
Or
'defaults'{
'guard' : 'web',
'passwords' : 'users',
}
来源:https://stackoverflow.com/questions/61170647/laravel-sanctum-can-be-use-multiauth-guard