Skip to content

Instantly share code, notes, and snippets.

@akoserwal
Last active January 31, 2024 12:52
Show Gist options
  • Save akoserwal/534c0b4d7f7db660a333e76ff0c4ee26 to your computer and use it in GitHub Desktop.
Save akoserwal/534c0b4d7f7db660a333e76ff0c4ee26 to your computer and use it in GitHub Desktop.
vue-integration-keycloak
let initOptions = {
url: 'https://0.0.0.0:8445/auth', realm: 'keycloak-demo', clientId: 'vue-test-app', onLoad:'login-required'
}
let keycloak = Keycloak(initOptions);
keycloak.init({ onLoad: initOptions.onLoad }).success((auth) =>{
if(!auth) {
window.location.reload();
} else {
Vue.$log.info("Authenticated");
}
new Vue({
render: h => h(App),
}).$mount('#app')
localStorage.setItem("vue-token", keycloak.token);
localStorage.setItem("vue-refresh-token", keycloak.refreshToken);
setInterval(() =>{
keycloak.updateToken(70).success((refreshed)=>{
if (refreshed) {
Vue.$log.debug('Token refreshed'+ refreshed);
} else {
Vue.$log.warn('Token not refreshed, valid for '
+ Math.round(keycloak.tokenParsed.exp + keycloak.timeSkew - new Date().getTime() / 1000) + ' seconds');
}
}).error(()=>{
Vue.$log.error('Failed to refresh token');
});
}, 60000)
}).error(() =>{
Vue.$log.error("Authenticated Failed");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment