'Keycloak-js refresh token in Vue.js 2 and Vuex
Hi everyone I have a problem with the keycloack refresh token and the keycloack-js v16.1.1 library, inside my application I have to make a page publicly accessible, to do everything I create the keycloak instance and assign this in a Vue plugin by myself created in authorization.js file:
// file authorization.js
const AuthPluginKeycloak = {
install() {
Vue.$keycloak = keycloak;
}
}
AuthPluginKeycloak.install = Vue => {
Vue.$keycloak = keycloak;
Object.defineProperties(Vue.prototype, {
$keycloak:{
get() {
return keycloak;
}
}
})
}
I then had to move the library init from main to the login page. However, I left the refresh token inside the main.js with a setInterval of 170000 and set the update to 150 seconds considering that the token expires in 300s:
// main.js
setInterval(() => {
Vue.$keycloak.updateToken(150)
.then( async (refreshed) => {
if (refreshed) {
store.commit("updateIsLoggedIn", true);
store.commit( "updateRefreshToken", this.$keycloak.refreshToken );
store.commit( "updateToken", Vue.$keycloak.idToken );
console.log('Token refreshed '+ refreshed);
http.defaults.headers.common = { 'Authorization': `Bearer ${ await Vue.$keycloak.idToken }` }
} else {
console.warn('Token not refreshed, valid for '
+ Math.round(Vue.$keycloak.tokenParsed.exp + Vue.$keycloak.timeSkew - new Date().getTime() / 1000) + ' seconds');
}
}).catch(()=>{
store.commit("updateIsLoggedIn", false);
store.commit("updateOrganizationGuid", "");
console.error('Failed to refresh token');
});
}, 170000 );
everything works fine including the refresh token, until I do a reload of the page, at which point the refresh token doesn't work anymore, could you help me please? I don't know where to turn.
Thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
