danivijay / react-commerce

Shopping cart made with ReactJS.
3 stars 2 forks source link

feat(token-expiry): handling the token expiry #53

Closed ashinpchandran closed 5 years ago

ashinpchandran commented 5 years ago

Added token verification to find out whether the token is expired or not and fixed the token expiration bug.

 //Token expiry verification
    var tokenisExpired = false;
    var tokendata = localStorage.getItem('AUTH_TOKEN');
    if (tokendata) {
        console.log('Tokendata==>>', tokendata);
        var decodedToken = jwt.decode(tokendata, { complete: true });
        console.log('decodedTokendata==>>', decodedToken.payload.exp * 1000);
        var dateNow = new Date();
        console.log('datenow.time===>', dateNow.getTime());

        //multiplying the token exp with 1000 to make the no.of digits of exp to 13 so that it can be compared with the dateNow.getTime()
        if (decodedToken.payload.exp * 1000 < dateNow.getTime())
            tokenisExpired = true;
        console.log('isTokenExpired=>>', tokenisExpired);
    }

Now if the token is expired it is removed from the local storage.