我的动作: 1)我安装了jwt-auth 2)创建控制器AuthController 3)在我发送AuthController中的方法loginUser和loginAdmin的路由中
但是jwt-auth尝试通过用户模型进行授权。
我的方法身份验证管理员:
public function adminLogin(Request $request){
Config::set('jwt.user', 'App\Admins');
Config::set('auth.providers.users.model', \App\Admins::class);
$credentials = $request->only('email', 'password');
$token = null;
try {
if (!$token = JWTAuth::attempt($credentials)) {
return response()->json([
'response' => 'error',
'message' => 'invalid_email_or_password',
]);
}
} catch (JWTAuthException $e) {
return response()->json([
'response' => 'error',
'message' => 'failed_to_create_token',
]);
}
return response()->json([
'response' => 'success',
'result' => [
'token' => $token,
'message' => 'I am Admin user',
],
]);
}
使用App \ Admins返回配置 但是JWT:Auth指的是用户模型,如何解决?
我的composer.json:
"require": {
"php": "^7.1.3",
"fideloper/proxy": "^4.0",
"laravel/framework": "5.6.*",
"laravel/tinker": "^1.0",
"tymon/jwt-auth": "^1.0.0-rc.2"
},
决定: guard plugun安装我的项目
我的方法代码:
public function adminLogin(Request $request){
Config::set('jwt.user', 'App\Admins');
Config::set('auth.providers.users.model', \App\Admins::class);
$credentials = $request->only('email', 'password');
$token = null;
try {
if (!$token = Auth::guard('jwt')->attempt($credentials)) {
return response()->json([
'response' => 'error',
'message' => 'invalid_email_or_password',
]);
}
} catch (JWTAuthException $e) {
return response()->json([
'response' => 'error',
'message' => 'failed_to_create_token',
]);
}
return response()->json([
'response' => 'success',
'result' => [
'token' => $token,
'message' => 'I am Admin user',
],
]);
}
这不是最佳的解决方案,但它可以工作并共享用户和管理员。