JWT/LARAVEL token expired

只愿长相守 提交于 2019-12-04 22:05:10

So you are using normal PHP methods for setting headers inside a Laravel middleware, that ain't going to work.

You should checko this out: https://github.com/tymondesigns/jwt-auth/blob/develop/src/Http/Middleware/BaseMiddleware.php

https://github.com/tymondesigns/jwt-auth/blob/develop/src/Http/Middleware/RefreshToken.php

Basically, change:

header('Authorization: Bearer ' . $refreshed);

to

$response = $next($request);
$response->headers->set('Authorization', 'Bearer '.$token);

The thing is the following, this aproach won't work as expected because this kind of "AfterMiddleware" gets executed after the request has been throught your app. So:

  1. User makes request with expired token
  2. The app will return a 403 or 401 (can't remember) because of TokenExpired.
  3. The response however will contain a header with the new token
  4. You make the same request with the new token and it should work.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!