Multiple User Models Laravel JWT Auth(多用户模型 Laravel JWT Auth)
问题描述
我必须在我的 eloquent 中使用模型:
I have to user models in my eloquent:
- 用户
- 办公用户
OfficeUser 在 JWT 配置中定义为标准模型.现在我已经编写了一个中间件来验证它们中的每一个
OfficeUser is in defined in the JWT config as standard model. Now I have written a Middleware for authenticate each of them
授权用户:
public function handle($request, Closure $next)
{
Config::set('auth.providers.users.model', AppUser::class);
try {
if (! $user = JWTAuth::parseToken()->authenticate()) {
return response()->json(['user_not_found'], 404);
}
} catch (TymonJWTAuthExceptionsTokenExpiredException $e) {
return response()->json(['token_expired'], $e->getStatusCode());
} catch (TymonJWTAuthExceptionsTokenInvalidException $e) {
return response()->json(['token_invalid'], $e->getStatusCode());
} catch (TymonJWTAuthExceptionsJWTException $e) {
return response()->json(['token_absent'], $e->getStatusCode());
}
return $next($request);
}
authOffice用户
authOfficeUser
public function handle($request, Closure $next)
{
try {
if (! $user = JWTAuth::parseToken()->authenticate()) {
return response()->json(['user_not_found'], 404);
}
} catch (TymonJWTAuthExceptionsTokenExpiredException $e) {
return response()->json(['token_expired'], $e->getStatusCode());
} catch (TymonJWTAuthExceptionsTokenInvalidException $e) {
return response()->json(['token_invalid'], $e->getStatusCode());
} catch (TymonJWTAuthExceptionsJWTException $e) {
return response()->json(['token_absent'], $e->getStatusCode());
}
return $next($request);
}
另外我为他们每个人都有一个登录功能:
Additionally I have a login function for each of them:
登录用户
if ($user){
if (Hash::check($request->password, $user->password)) {
// grab credentials from the request
$credentials = $request->only('email', 'password');
try {
// attempt to verify the credentials and create a token for the user
Config::set('auth.providers.users.model', AppUser::class);
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}
登录办公室用户
if ($user){
if (Hash::check($request->password, $user->password)) {
// grab credentials from the request
$credentials = $request->only('email', 'password');
try {
// attempt to verify the credentials and create a token for the user
Config::set('auth.providers.users.model', AppOfficeUser::class);
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}
不幸的是,当我登录并尝试调用 authUser 中间件后面的路由时,我得到一个user_not_found"
Unfortunately when I login and try to call a route behind the authUser Middleware I get an "user_not_found"
有人知道为什么会这样吗?OfficeUser 身份验证工作正常
Does anybody have an idea why this happens? OfficeUser authentication works fine
推荐答案
发帖给任何发现此问题的人
虽然不建议有两个用户表,但我有一个类似的要求,即与我们的一个客户设置 JWT.这就是我解决问题的方法.
Posting for anyone who finds this questions
Although it's not recommended to have two user tables, but I had a similar requirement of setting up JWT with one of our clients. This is how I solved the issue.
无需对 `config/auth.php' 中的提供者进行任何更改
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => AppUser::class,
],
]
在您的身份验证控制器中,通过设置动态修改提供者使用的模型
In your authentication controller, dynamically modify the model used by the providers by setting
Config::set('auth.providers.users.model', AppTrainer::class);
示例代码
在 authenticate() 方法中
if ($credentials['user_type'] == 'consultant') {
Config::set('auth.providers.users.model', AppTrainer::class);
} else {
Config::set('auth.providers.users.model', AppUser::class);
}
//Find the user
//Create the token
if ($user) {
$customClaims = ['user_type' => $credentials['user_type']];
$token = JWTAuth::fromUser($user,$customClaims);
} else {
return response()->json(['error' => 'invalid_credentials'], 401);
}
在解析令牌以验证用户身份时,您也必须这样做.示例代码
You will have to do the same while parsing the token to authenticate the user as well. Example code
在 getAuthenticatedUser() 方法中
$payload = JWTAuth::parseToken()->getPayload();
$user_type = $payload->get('user_type');
if($user_type === 'consultant'){
Config::set('auth.providers.users.model', AppTrainer::class);
}else{
Config::set('auth.providers.users.model', AppUser::class);
}
if (!$user = JWTAuth::parseToken()->authenticate()) {
return response()->json(['user_not_found'], 404);
}
这篇关于多用户模型 Laravel JWT Auth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:多用户模型 Laravel JWT Auth
基础教程推荐
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
