php - 在 laravel 中自定义守卫尝试方法
问题描述
我想为来自同一个表但角色不同的用户实现多重身份验证。我的目标是,如果用户正在从手机查看,系统搜索角色 = 用户的用户对象,如果它来自 pc,系统搜索角色 = 管理员的用户对象。
为此,我在 config/auth.php 中添加了 2 个自定义保护:
'admin' => [
'driver' => 'custom-admin',
'provider' => 'users',
],
'user' => [
'driver' => 'custom-user',
'provider' => 'users',
],
我还将逻辑添加到 LoginController
protected function attemptLogin(Request $request)
{
$is_mobile = (isset($request->is_mobile) && $request->is_mobile == 'true') ? true : false;
if($is_mobile == true)
{
return $this->guard('user')->attempt(
$this->credentials($request), $request->filled('remember')
);
}
else
{
return $this->guard('admin')->attempt(
$this->credentials($request), $request->filled('remember')
);
}
}
我还尝试修改 AuthServiceProvider boot() 方法中的守卫:
Auth::viaRequest('custom-admin', function ($request) {
return User::where('email', $request->email)->where('password', Hash::make($request->password))->where('role', 'admin')->first();
});
Auth::viaRequest('custom-user', function ($request) {
return User::where('email', $request->email)->where('password', Hash::make($request->password))->where('role', 'user')->first();
});
我的问题是如何覆盖警卫的尝试方法,以便它使用我需要的逻辑?我错过了什么,我还应该定义什么?
我在这种状态下收到的当前错误:
Method Illuminate\Auth\RequestGuard::attempt does not exist.
先感谢您
解决方案
推荐阅读
- karate - 空手道 DSL:从响应中提取 XML 属性值
- html - Flex nowrap 容器不包裹孩子。当那些的宽度以百分比提到时
- github - 如果前一个失败但当前一个成功,有什么方法可以触发 Github 操作?
- flutter - 如何使用 Provider 实现注册/登录逻辑。颤振,飞镖,移动开发
- java - 当所有其他消息完成处理时,Spring集成处理消息
- json - 通过 Oauth 请求 Zoom API 的访问令牌 - 错误“缺少授权类型”
- node.js - MongoDb find().lean() 很慢
- linux - 'apt-get clean' 在带有 WSL2 的 Ubuntu 20.04 LST 中返回错误
- vue.js - 如何在没有直接访问的情况下模拟 vuex 函数?
- xamarin.forms - 使用 Prism NavigationService 实例化选项卡页面子视图模型的问题