本文介绍了Laravel Sanctum可以使用Multiauth保护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在与laravel密室进行测试,但是这里有一些问题.我正在创建管理员后卫.
I'm testing with laravel sanctum but here some issues..I'm creating Admin guard.
当我将中间件更改为auth:sanctum_admin ..时,它只能由admin访问,但是在这里,我可以使用具有Web Guard的普通用户帐户进行访问.我不知道为什么?...我将护照与multiauth包一起使用.很好.但是在圣所中不能将用户表和管理员分开.
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.
推荐答案
您也可以在圣所中使用多个守卫.为此,请按照以下步骤-
You can, also use multiple guards in sanctum. To achieve this, follow these steps -
- 根据需要创建自己的后卫.(在
config/auth.php
中)
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
]
],
- 设置提供者.(在
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();
}
这篇关于Laravel Sanctum可以使用Multiauth保护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!