export const refreshAccessToken = async () => {
  console.log("Refresh Access Token start");
  const refreshToken = localStorage.getItem("refreshToken");
  if (!refreshToken) {
    console.log("Refresh token not found");
    return null;
  }

  try {
    const response = await fetch(
      `https://server-ancient-grass-9030.fly.dev/api/token`,
      {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({ refreshToken }),
      }
    );

    if (response.ok) {
      const data = await response.json();
      const newAccessToken = data.accessToken;
      localStorage.setItem("accessToken", newAccessToken);
      return newAccessToken;
    } else {
      console.log("Failed to refresh access token");
      return null;
    }
  } catch (error) {
    console.error("An error occurred while refreshing access token:", error);
    return null;
  }
};

Вот код обновления токена

Вот происходящая ошибка в приложении:
POST https://server-ancient-grass-9030.fly.dev/api/login 401 (Unauthorized)
mm @ index-RR6aZbZD.js:75
(anonymous) @ index-RR6aZbZD.js:75
(anonymous) @ index-RR6aZbZD.js:77
(anonymous) @ index-RR6aZbZD.js:77
jl @ index-RR6aZbZD.js:40
on @ index-RR6aZbZD.js:40
(anonymous) @ index-RR6aZbZD.js:40
E @ index-RR6aZbZD.js:25
yn @ index-RR6aZbZD.js:25
index-RR6aZbZD.js:75 Refresh Access Token start
index-RR6aZbZD.js:76 
        
        
       GET https://server-ancient-grass-9030.fly.dev/api/cards 403 (Forbidden)
(anonymous) @ index-RR6aZbZD.js:76
(anonymous) @ index-RR6aZbZD.js:77
(anonymous) @ index-RR6aZbZD.js:77
jl @ index-RR6aZbZD.js:40
on @ index-RR6aZbZD.js:40
(anonymous) @ index-RR6aZbZD.js:40
E @ index-RR6aZbZD.js:25
yn @ index-RR6aZbZD.js:25
index-RR6aZbZD.js:75 {message: 'Invalid token'}
index-RR6aZbZD.js:75 {message: 'Invalid token'}
index-RR6aZbZD.js:40 TypeError: e.map is not a function
    at dm (index-RR6aZbZD.js:75:3819)
    at Bi (index-RR6aZbZD.js:38:16959)
    at Vc (index-RR6aZbZD.js:40:43730)
    at Ac (index-RR6aZbZD.js:40:39531)
    at Dp (index-RR6aZbZD.js:40:39462)
    at ml (index-RR6aZbZD.js:40:39320)
    at oi (index-RR6aZbZD.js:40:35737)
    at Dc (index-RR6aZbZD.js:40:34693)
    at E (index-RR6aZbZD.js:25:1562)
    at MessagePort.yn (index-RR6aZbZD.js:25:1930)
overrideMethod @ console.js:288
Yo @ index-RR6aZbZD.js:40
n.callback @ index-RR6aZbZD.js:40
qu @ index-RR6aZbZD.js:38
hs @ index-RR6aZbZD.js:40
Fc @ index-RR6aZbZD.js:40
Ip @ index-RR6aZbZD.js:40
$p @ index-RR6aZbZD.js:40
Nt @ index-RR6aZbZD.js:40
Dc @ index-RR6aZbZD.js:40
E @ index-RR6aZbZD.js:25
yn @ index-RR6aZbZD.js:25
Show 1 more frame
Show less
index-RR6aZbZD.js:75 Uncaught TypeError: e.map is not a function
    at dm (index-RR6aZbZD.js:75:3819)
    at Bi (index-RR6aZbZD.js:38:16959)
    at Vc (index-RR6aZbZD.js:40:43730)
    at Ac (index-RR6aZbZD.js:40:39531)
    at Dp (index-RR6aZbZD.js:40:39462)
    at ml (index-RR6aZbZD.js:40:39320)
    at oi (index-RR6aZbZD.js:40:35737)
    at Dc (index-RR6aZbZD.js:40:34693)
    at E (index-RR6aZbZD.js:25:1562)
    at MessagePort.yn (index-RR6aZbZD.js:25:1930)
dm @ index-RR6aZbZD.js:75
Bi @ index-RR6aZbZD.js:38
Vc @ index-RR6aZbZD.js:40
Ac @ index-RR6aZbZD.js:40
Dp @ index-RR6aZbZD.js:40
ml @ index-RR6aZbZD.js:40
oi @ index-RR6aZbZD.js:40
Dc @ index-RR6aZbZD.js:40
E @ index-RR6aZbZD.js:25
yn @ index-RR6aZbZD.js:25
backendManager.js:1 browsecSiteContentScript: data is not for auth
(anonymous) @ browsecSiteContentScript.js:1
postMessage
send @ backendManager.js:1
(anonymous) @ bridge.js:387
setTimeout
send @ bridge.js:329
(anonymous) @ agent.js:805
(anonymous) @ hook.js:433
emit @ hook.js:433
flushOrQueueOperations @ renderer.js:1688
flushPendingEvents @ renderer.js:1927
handleCommitFiberRoot @ renderer.js:2822
onCommitFiberRoot @ hook.js:471
pd @ index-RR6aZbZD.js:37
$p @ index-RR6aZbZD.js:40
Nt @ index-RR6aZbZD.js:40
Dc @ index-RR6aZbZD.js:40
E @ index-RR6aZbZD.js:25
yn @ index-RR6aZbZD.js:25
Show 10 more frames
Show less
debug.js:89 (3)(+0010867): Connector_Browser: onFrameLoaded for https://pavel-buiko.github.io/ClientSpring/; https://pavel-buiko.github.io/ClientSpring/
debug.js:92 (3)(+0000000): Connector_Browser: URL changed from https://pavel-buiko.github.io/ClientSpring/login to https://pavel-buiko.github.io/ClientSpring/
debug.js:92 (3)(+0000067): historyChanged message received in injected page https://pavel-buiko.github.io/ClientSpring/
index-RR6aZbZD.js:75 
        
        
       POST https://server-ancient-grass-9030.fly.dev/api/token 403 (Forbidden)
au @ index-RR6aZbZD.js:75
mm @ index-RR6aZbZD.js:75
await in mm
(anonymous) @ index-RR6aZbZD.js:75
(anonymous) @ index-RR6aZbZD.js:77
(anonymous) @ index-RR6aZbZD.js:77
jl @ index-RR6aZbZD.js:40
on @ index-RR6aZbZD.js:40
(anonymous) @ index-RR6aZbZD.js:40
E @ index-RR6aZbZD.js:25
yn @ index-RR6aZbZD.js:25
index-RR6aZbZD.js:75 Failed to refresh access token

import jwt from "jsonwebtoken";
import dotenv from "dotenv";

dotenv.config();

const ACCESS_TOKEN_SECRET = process.env.ACCESS_TOKEN_SECRET;

export const authenticateToken = (req, res, next) => {
  const authHeader = req.headers["authorization"];
  const token = authHeader && authHeader.split(" ")[1];

  if (!token) return res.status(401).json({ message: "Token not provided" });

  //Error: Invalid token
  jwt.verify(token, ACCESS_TOKEN_SECRET, (err, user) => {
    if (err) return res.status(403).json({ message: "Invalid token" });
    req.user = user;
    next();
  });
};
Вот как мы это вызываем