existing middleware:
"jwt.auth","jwt.check","jwt.refresh","jwt.renew"
either authenticate toekn (jwt.auth) directly or refresh toekn (jwt.refresh) directly
so how to determine whether the toekn has expired, and if so, then refresh it?
existing middleware:
"jwt.auth","jwt.check","jwt.refresh","jwt.renew"
either authenticate toekn (jwt.auth) directly or refresh toekn (jwt.refresh) directly
so how to determine whether the toekn has expired, and if so, then refresh it?
add your own middleware
example:
<?php
namespace App\Http\Middleware;
use Closure;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Http\Middleware\BaseMiddleware;
class RefreshToken extends BaseMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$newToken = null;
$this->auth->unsetToken();
$this->checkForToken($request);
try {
$user = $this->auth->parseToken()->authenticate();
if (!$user) {
return response()->json([
'status_code' => 401,
'message' => '',
'time' => time(),
], 401);
}
} catch (TokenExpiredException $e) {
try {
$newToken = $this->auth->refresh();
$request->headers->set('Authorization', 'Bearer ' . $newToken);
} catch (JWTException $e) {
//
return response()->json([
'status_code' => 401,
'message' => '',
'error' => $e->getMessage(),
'time' => time(),
], 401);
}
} catch (JWTException $e) {
return response()->json([
'status_code' => 401,
'message' => '.',
'error' => $e->getMessage(),
'time' => time(),
], 401);
}
$response = $next($request);
if ($newToken) {
$response->headers->set('Authorization', 'Bearer ' . $newToken);
}
return $response;
}
}
other people's blog: https://www.jianshu.com/p/9e9.
misread the time and thought it was 19 years. A year ago.
Previous: Postman request API displays cross-domain
Next: Why does vscode search only files under the current project?
I have referred to the official document http: jwt-auth.readthedocs.i. for configuration and returned as a result. { "token":"bearer 1" } I really don t know why token returned to true., to ask for answers ...
ready to develop Mini Program, first test the background API, project to develop using Laravl+jwt+dingo. JWT wants to find such a middleware on the Internet during painless refresh token, testing public function handle($request, Closure $next) ...