Open danishkhan3621 opened 1 year ago
This works for me
In my response there will always be the access_token
`<?php
namespace App\Http\Middleware;
use Closure; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Symfony\Component\HttpFoundation\Response; use Tymon\JWTAuth\Facades\JWTAuth; use Illuminate\Support\Facades\Response as Res; use Tymon\JWTAuth\Exceptions\TokenBlacklistedException; use Tymon\JWTAuth\Exceptions\TokenExpiredException; use Tymon\JWTAuth\Exceptions\TokenInvalidException;
class JWT { /**
@param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next */ public function handle( Request $request, Closure $next ): Response | JsonResponse { try { /* / JWTAuth::parseToken()->authenticate();
/** */
return $next($request);
} catch (\Throwable $th) {
if ($th instanceof TokenExpiredException) {
/** */
$refreshed = JWTAuth::refresh();
/** */
$user = JWTAuth::setToken($refreshed)->toUser();
/** */
$response = $next($request);
/** */
$response->headers->set('access_token', $refreshed);
/** */
return $response;
}
if ($th instanceof TokenInvalidException) {
/** */
return Res::json(["message" => "El token es invalido", "type" => "token/invalid"], 401);
}
if ($th instanceof TokenBlacklistedException) {
/** */
return Res::json(["message" => "El token ya no esta en funcionamiento", "type" => "token/blacklisted"], 401);
}
/** */
return Res::json(["message" => "No se encontro el token", "type" => "token/notfound"], 401);
}
} } `
then I get the headers of the response of my request and I store it in localstsorage
I'm using JWT in Laravel 7 It's config file is
Created middleware :
In my case if i set 'ttl' => env('JWT_TTL',5), to 5 then my token is blacklisted and i'm getting
Tymon\JWTAuth\Exceptions\TokenBlacklistedException: The token has been blacklisted in file
and it is not refreshed