Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 401访问和刷新令牌的状态处理,节点_Javascript_Node.js_Reactjs_Jwt - Fatal编程技术网

Javascript 401访问和刷新令牌的状态处理,节点

Javascript 401访问和刷新令牌的状态处理,节点,javascript,node.js,reactjs,jwt,Javascript,Node.js,Reactjs,Jwt,当访问令牌消失时,我不知道如何处理来自服务器的401状态请求。 我在服务器端编写了accessTokenVerify: require('dotenv').config(); const jwt = require("jsonwebtoken"); // Access Token Verify exports.accessTokenVerify = (req, res, next) => { if (!req.headers.authorizatio

当访问令牌消失时,我不知道如何处理来自服务器的401状态请求。 我在服务器端编写了accessTokenVerify:

    require('dotenv').config();
const jwt = require("jsonwebtoken");

//  Access Token Verify
exports.accessTokenVerify = (req, res, next) => {
    if (!req.headers.authorization) return res.status(401).send({ msg: "Access Token is missing." })

    const BEARER = 'Bearer';
    const auth_token = req.headers.authorization.split(' ');
    if (auth_token[0] !== BEARER) return res.status(401).send({ msg: "Access Token is not complete." })

    jwt.verify(auth_token[1], process.env.ACCESS_TOKEN_SECRET, (err) => {
        if (err) return res.status(401).send({ msg: "Access Token is invalid." })
        next();
    })
}
我有刷新访问令牌端点localhost:5000/api/auth/refresh。 但我不知道如何在客户端实现请求,当我得到401状态响应时,客户端会发送刷新请求

到目前为止,我有:

const onSubmit = (values: Values) => {
 
    fetch("http://localhost:5000/api/posts", {
      method: "POST",
      credentials: "include",
      headers: {
        "Content-Type": "application/json",
        // 'Content-Type': 'application/x-www-form-urlencoded',
      },
 
      body: JSON.stringify({
        name: "Nazwa",
        text: "jakiś post",
        id: "341324132"
      }),
    })
      .then((response) => {
        if (response.status == 401) {
          refreshTokens();
          [and what next...]
        }
        return response.json();
      })
      .then((resp) => console.log(resp))
      .catch((err) => {
        console.log(err);
      });
  };

当refreshTokens()将被发送到/RefreshEndpoint并设置新的令牌时,但我不知道如何重新发送主请求(添加post)。

实际上我自己也解决了类似的情况。因此,我强烈建议通过Axios管理您的api请求。从这里,您可以使用
axios拦截器
解析请求的响应和错误,相应地处理并使用
axios.request(error.config)
重试请求。如果您在使用
async/await
时没有任何问题,您可以像
let resp=await fetch(…)那样实现它;if(resp.status==401){await refereshtokens();resp=await fetch(…);}
。(您可以通过在不同的函数中写入递归/重试来避免重复)。