[英]laravel 5.1 Auth::check() 500 internal server error afterJWTAuth::attempt($credentials) login
我在laravel 5.1中创建了登录api。
我在控制器中创建了用户身份验证
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);
它正在工作,并成功生成令牌。
但是在返回此代码之前,如果检查登录dd(Auth::check());
它抛出
状态500内部服务器错误
然后在这里,如何在成功登录后从数据库中获取用户?
我真的不知道Auth :: check()是否会在laravel 5.1中默认工作,因为JWTAUTH和Auth是两个不同的接口。 所以我做的是我有一个功能,我用来检查和获取用户登录时的用户详细信息。
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;
}
检查你是否做了一些登录该功能,检查你是否拥有相对日志文件的权限。 Log :: info可能会抛出500服务器错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.