KEYCLOAK - Refresh/update token not working
I couldn't find explained it in the API docs but the timeout
argument of keycloak.updateToken()
function is expressed in seconds, not in minutes.
So if the Access Token Lifespan on server is at the default value of 5 minutes, you should use a value less than 300 seconds. I learned it doing some experiments.
//Update the token when will last less than 3 minutes
keycloak.updateToken(180)
Btw I suggest you to use a Lifespan longer than 5 minutes for the token.
In your code You never see the token refreshed because the refresh is never triggered in the 15 seconds window in which will work.
(refreshed) returns false only if your token is not expired. So you're trying to refresh the token when it has not yet expired.
set " Access Token Lifespan " to 1 minute in the Keycloak realm you're using, then try the following code to check the status of refreshed again
keycloak.onTokenExpired = ()=>{
console.log('expired '+new Date());
keycloak.updateToken(50).success((refreshed)=>{
if (refreshed){
console.log('refreshed '+new Date());
}else {
console.log('not refreshed '+new Date());
}
}).error(() => {
console.error('Failed to refresh token '+new Date());
});
}