function Auth() {
this.accessTokenError = false;
}
Auth.prototype.validateToken = function (accessToken, refreshToken) {
var token;
var self = this;
return new Promise(function (resolve, reject) {
AuthCron.secret()
.then(function (secret) {
if (self.accessTokenError) {
token = refreshToken;
secret = secret.substr(0, secret.length / 2);
}
else
token = accessToken;
JWT.verify(token, secret, function (error, decoded) {
console.log(error, decoded);
if (error) {
if (!self.accessTokenError) {
self.accessTokenError = true;
// I don't know how to handle this
self.validateToken(accessToken, refreshToken)
}
else {
self.accessTokenError = false;
reject(error);
}
}
else
resolve(decoded.user);
});
})
.catch(function (err) {
reject(err)
});
})
};
I am bit confused on how to handle recursive promise. The problem here is first promise never resolves nor rejects. What is the best way to handle this situation? This function accepts two tokens if access token is expired or invalid then refresh token is validated, if refresh token is also invalid then promise should reject.