How to implement laravel jwt middleware: first determine whether the toekn has expired, if so, and 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?

Mar.03,2021

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.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3615b-2bffd.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3615b-2bffd.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?