【发布时间】:2016-04-22 11:44:48
【问题描述】:
我的 laravel-Angular 应用程序遇到了非常奇怪的问题。我正在使用 Tymon JWT 在我的每个请求上刷新令牌。我正在使用 Satellizer 库来处理这些 JWT 令牌,但是,Satellizer 似乎没有响应拦截器来捕获新令牌。因此,我编写了自己的拦截器来这样做。
.factory('ResponseHttpInterceptor', function ($window) {
return {
response: function (response) {
if (response.headers('Authorization') != null) {
$window.localStorage.removeItem('satellizer_token');
$window.localStorage.setItem('satellizer_token', response.headers('Authorization').replace('Bearer ', ''));
}
return response;
}
}
})
此代码基本上捕获新令牌并用新令牌替换本地存储中的现有令牌。
我的测试流程是:
Login -> Make who Am I call -> Logout
注销时我收到错误Invalid token(这并不总是发生。有时流程成功,有时失败)。此流程通过 REST 客户端邮递员完美运行。所以我认为我的 API 没有任何问题
在我的whoami 调用后刷新后,附加图像显示正在传递的新令牌。
注销后,我正在清除本地存储。谁能告诉我这可能是什么原因?
编辑
Route::group(['prefix' => 'api/v1_0'], function () {
Route::post('login', 'Auth\AuthControllerGeneral@postLogin');
Route::get('logout', ['middleware' => 'jwt.auth', 'uses' => 'Auth\AuthControllerGeneral@getLogout']);
Route::group(['middleware' => ['jwt.refresh', 'jwt.auth']], function() {
Route::get('whoami', 'Auth\AuthControllerGeneral@loggedInUserInfo');
});
});
【问题讨论】:
-
分享你的 htcacess 和 auth 控制器
-
不在生产服务器上。使用
PHP artisan serve运行服务器。 -
为什么需要
AuthController?我的意思是,这是一个通用的控制器。我会分享我的路线。请查看编辑。
标签: angularjs laravel jwt angular-http-interceptors satellizer