[英]laravel 5.1 Auth::check() 500 internal server error afterJWTAuth::attempt($credentials) login
I am creating login api in laravel 5.1 . 我在laravel 5.1中创建了登录api。
I have created user authentication in controller 我在控制器中创建了用户身份验证
try {
// verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
$response['error'] = 'true';
$response['data'] = 'invalid_credentials';
return response()->json($response);
}
} catch (JWTException $e) {
// something went wrong
$response['error'] = 'true';
$response['data'] = 'could_not_create_token';
return response()->json($response);
}
$response['error'] = 'false';
$response['data'] = ['token'=>$token];
return response()->json($response);
It is working, and successfully generating token. 它正在工作,并成功生成令牌。
but in this code before return, if check login dd(Auth::check());
但是在返回此代码之前,如果检查登录dd(Auth::check());
It throws 它抛出
Status 500 Internal Server Error 状态500内部服务器错误
Then here, How can I get user from database after successful login? 然后在这里,如何在成功登录后从数据库中获取用户?
I dont really know if Auth::check() will work by default in laravel 5.1 since JWTAUTH and Auth are two different interfaces. 我真的不知道Auth :: check()是否会在laravel 5.1中默认工作,因为JWTAUTH和Auth是两个不同的接口。 so what i do is i have a function that i use to check and get the user details if the user is logged in. 所以我做的是我有一个功能,我用来检查和获取用户登录时的用户详细信息。
public function getAuthUser() {
try {
if (! $user = JWTAuth::parseToken()->authenticate()) {
throw new Exception('user_not_found',404);
}else{
$data['user'] = $user;
}
} catch (TokenExpiredException $e) {
throw new Exception('token_expired',$e->getStatusCode());
} catch (TokenInvalidException $e) {
throw new Exception('invalid_token',$e->getStatusCode());
} catch (JWTException $e) {
throw new Exception($e->getMessage(),403);
throw new Exception('token_absent',$e->getStatusCode());
}
// the token is valid and we have found the user via the sub claim
return $data;
}
check if you done some Log in the function, check if you have the right permission on the relative log file. 检查你是否做了一些登录该功能,检查你是否拥有相对日志文件的权限。 Log::info may throw the 500 server error. Log :: info可能会抛出500服务器错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.