Angular - 刷新jwt标记

时间:2016-06-13 19:59:35

标签: angularjs laravel jwt

我在Laravel有一个后端并在Ionic中制作应用程序。我在前端使用satellizer,在laravel端使用this package进行JWT身份验证。由于我需要为用户提供令牌,直到他们注销,我需要一种方法来刷新他们的令牌。我不知道如何做到这一点,让所有路由请求auth令牌并发送401响应(如果不存在),然后执行令牌刷新,或者在前端端使用计时器刷新令牌以及如何实际执行此操作会更好?

这是我的路线档案:

Route::group(['jwt.auth', ['except' => ['authenticate']], 'prefix' => 'api', 'namespace' => 'Api'], function() {
  Route::post('authenticate', 'AuthenticateController@authenticate');
  Route::get('user', 'AuthenticateController@getAuthenticatedUser');
  Route::get('comment', 'AuthenticateController@comment');
  Route::get('articles/latest', 'ArticlesController@latest');
  Route::get('articles/by-score', 'ArticlesController@byScore');
  Route::get('article/{id}', 'ArticlesController@show');
  Route::get('comments', 'CommentsController@index');
  Route::get('comments/{id}', 'CommentsController@show');
  Route::post('comments/create', 'CommentsController@store');
  Route::put('comments/update', 'CommentsController@update');
  Route::delete('comments/delete', 'CommentsController@destroy');
  Route::post('article/upvote', 'VotesController@upvote');
  Route::delete('article/upvote/delete', 'VotesController@destroyUpVote');
  Route::post('article/challenge/vote', 'VotesController@challengeVote');
  Route::delete('article/challenge/vote/delete', 'VotesController@destroyChallengeVote');
});

1 个答案:

答案 0 :(得分:1)

我过去所做的是对每个请求进行授权,然后在数据库中令牌到期的某个时间段内创建新令牌。对我来说,如果用户在过去15分钟内仍处于活动状态,则会刷新1小时令牌。然后,如果令牌被刷新,则在Laravel中设置新令牌并使旧令牌无效。或者您可以只延长数据库中的刷新时间。我的建议是在最初创建令牌时设置一些expire_time。如果令牌过期,则返回401。