【发布时间】:2018-03-04 09:16:59
【问题描述】:
我正在尝试使用 Tymon 的 JWTAuth 在 Laravel 5.5 中实现基于令牌的身份验证。我遵循库的GitHub Documentation 并使用以下身份验证流程。这是我的登录路径的身份验证部分:
try {
// attempt to verify the credentials and create a token for the user
if (!$token = JWTAuth::attempt($credentials)) {
return response()->json(['success' => false, 'error' => 'Invalid Credentials. Please make sure you entered the right information and you have verified your email address.'], 401);
}
}
catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['success' => false, 'error' => 'could_not_create_token'], 500);
}
// all good so return the token
return response()->json(['success' => true, 'data'=> [ 'token' => $token ]]);
以下是路线:
Route::group([
'middleware' => ['jwt.auth', 'jwt.refresh'],
],
function () {
// Routes requiring authentication
Route::get('/logout', 'Auth\LoginController@logout');
Route::get('/protected', function() {
return 'This is a protected page. You must be logged in to see it.';
});
});
所以你可以看到我正在使用 jwt.auth 和 jwt.refresh 中间件。现在,一切似乎都按预期工作,我可以使用令牌对用户进行身份验证。每个令牌都有一次使用的生命周期,并且在每次请求(刷新流程)之后,我都会获得另一个有效令牌。
但是,我的问题是,如果我有一个尚未使用的用户的有效令牌,然后我将其从标头中删除并使用有效凭据点击 /login 路由,我会收到 另一个 有效令牌。所以现在我有两个可用于验证用户身份的有效令牌,因为我的 /login 路由不会使之前发布的令牌失效。
有没有人知道一种方法来检查用户是否有一个未完成的有效令牌,以便在用户从其他地方登录时使其失效?
【问题讨论】:
标签: php laravel jwt restful-authentication