为什么我看不到此axios发布请求的响应?

时间:2020-01-13 09:19:42

标签: javascript post jwt axios

我正在进行服务以验证我的访问令牌。如果我使用邮递员,我将收到3种类型的答案,并且使用伪代码可以像这样工作:

//where response.data.status is the response received by my axios post to my backend
    if(response.data.status === "valid"){
      //allows access 
      return true
    }else if(response.data.status === "jwt expired"){
      //refresh the token
      //receive a new acces token
      //verify again the freshly received access token and return true or false based on this token verification
    }else{
      //every other case
      return false
    }

但是问题是我的(response.data.status === "jwt expired" )永远不会发生,这就是邮递员在我看到过期的访问令牌时返回的响应。 我不明白为什么无论何时else if(response.data.status === "jwt expired"必须发生,如果a console.log(response.data.status)返回未定义,axios都不会检测到该响应。同时我可以console.log正确记录其他情况

这是我实际代码的外观

    export default function isAuthenticated() {
    var accesstoken = Cookies.get('accesstoken');

    return axios({ method: 'post', url: 'http://localhost:3003/verify', headers: { Authorization: `Bearer ${accesstoken}` } })
        .then(function (response) {
            console.log("initial status of the token: "+response.data.status) //this will return undefined when the case of "jwt expired" must happen, the other ones work properly
            if (response.data.status === "valid") {
                //case 1 : token valid
                console.log("service valid, token didnt need refreshing")
                return true;
            } else if (response.data.status === "jwt expired") {
                //case 2 : token expired   
                //jwt is expired sends the token to refresh server and returns true or false based on refreshing process
                console.log("token is expired, proceds to refresh")
                axios({ method: 'post', url: 'http://localhost:3003/refresh_token', headers: { Authorization: `Bearer ${accesstoken}` } })
                    .then(function (response) {
                        accesstoken = response.data.accesstoken;
                        //once ive got the accesstoken return false if failed to refresh or redo the verification
                        if (accesstoken != "") {
                            axios({ method: 'post', url: 'http://localhost:3003/verify', headers: { Authorization: `Bearer ${accesstoken}` } })
                                .then(function (response) {
                                    if(response.data.status === "valid"){
                                        console.log("token refreshed and service valid returned true");
                                        return true;
                                    }else{
                                        console.log("refresh token attempted refresh but returned false")
                                        return false;
                                    }
                                })
                        } else {
                            console.log("refresh token failed to refresh and returned false")
                            return false;
                        }
                    })
            } else {
                //case 3 : other cases, token invalid / malformed...  
                console.log("service invalid")
                return false;
            }
        }).catch((error) => { //wont return anything
        if (error.response) {
            console.log(error.response.data);
            console.log(error.response.status);
            console.log(error.response.headers);
        } else if (error.request) {
            console.log(error.request);
        } else {
            console.log('Error', error.message);
        }
        console.log(error.config);
    });
}

1 个答案:

答案 0 :(得分:0)

axios本身没有任何问题。但是我检查响应不正确

因此,else if (response.data.status === "jwt expired")必须更改为else if (response.data.error === "jwt expired"),这样我才能看到消息