简体   繁体   中英

Why am I unable to see the response of this axios post request?

Im doing a Service to verify my access tokens. If I use postman, I will receive 3 types of answers and in pseudocode it would work like this:

//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
    }

But the issue is that my (response.data.status === "jwt expired" ) will never happen altho thats is the response that postman returns if I seend an expired access token. I dont understand why axios wont detect that response, whenever the else if(response.data.status === "jwt expired" must happen, if i console.log(response.data.status) it returns undefined. Meanwhile i can console.log the other cases properly

This is how my actual code looks

    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);
    });
}

There wasnt any issue with axios per se. But i was checking the response unproperly

So else if (response.data.status === "jwt expired") had to be changed to else if (response.data.error === "jwt expired") So i could see the message

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM