[英]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.