2016-06-13 220 views
0

我在Laravel中创建了一个后端,并在离子中创建了一个应用程序。我在前端使用satellizer,并在laravel端使用this package进行JWT身份验证。由于我需要在用户注销之前拥有令牌,所以我需要一种方式来以某种方式刷新令牌。我不知道如何做到这一点,让所有路由请求auth令牌并发送401响应(如果不存在),然后执行令牌刷新或刷新前端定时器的令牌以及如何实际执行?角度 - 刷新jwt令牌

这是我的路线文件:

Route::group(['jwt.auth', ['except' => ['authenticate']], 'prefix' => 'api', 'namespace' => 'Api'], function() { 
    Route::post('authenticate', '[email protected]'); 
    Route::get('user', '[email protected]'); 
    Route::get('comment', '[email protected]'); 
    Route::get('articles/latest', '[email protected]'); 
    Route::get('articles/by-score', '[email protected]'); 
    Route::get('article/{id}', '[email protected]'); 
    Route::get('comments', '[email protected]'); 
    Route::get('comments/{id}', '[email protected]'); 
    Route::post('comments/create', '[email protected]'); 
    Route::put('comments/update', '[email protected]'); 
    Route::delete('comments/delete', '[email protected]'); 
    Route::post('article/upvote', '[email protected]'); 
    Route::delete('article/upvote/delete', '[email protected]'); 
    Route::post('article/challenge/vote', '[email protected]'); 
    Route::delete('article/challenge/vote/delete', '[email protected]'); 
}); 

回答

1

我在过去所做的那样是做授权的请求,然后就创建一个新的令牌当令牌期满您数据库在一段时间内。对于我来说,如果用户在最近15分钟内仍然活跃,则会刷新1小时令牌。然后,如果令牌刷新,请在Laravel中设置新令牌并使旧令牌无效。或者你可以延长数据库的刷新时间。我的建议是在令牌最初创建时设置一些expire_time。如果令牌过期,则返回401。