Created
May 17, 2019 04:34
-
-
Save akoserwal/71e53706d080fb5c9d427f497c937b5d to your computer and use it in GitHub Desktop.
angular-keycloak-integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//keycloak init options | |
let initOptions = { | |
url: 'https://0.0.0.0:8445/auth', realm: 'keycloak-demo', clientId: 'angular-test-app' | |
} | |
let keycloak = Keycloak(initOptions); | |
keycloak.init({ onLoad: "login-required" }).success((auth) => { | |
if (!auth) { | |
window.location.reload(); | |
} else { | |
console.log("Authenticated"); | |
} | |
//bootstrap after authentication is successful. | |
platformBrowserDynamic().bootstrapModule(AppModule) | |
.catch(err => console.error(err)); | |
localStorage.setItem("ang-token", keycloak.token); | |
localStorage.setItem("ang-refresh-token", keycloak.refreshToken); | |
setTimeout(() => { | |
keycloak.updateToken(70).success((refreshed) => { | |
if (refreshed) { | |
console.debug('Token refreshed' + refreshed); | |
} else { | |
console.warn('Token not refreshed, valid for ' | |
+ Math.round(keycloak.tokenParsed.exp + keycloak.timeSkew - new Date().getTime() / 1000) + ' seconds'); | |
} | |
}).error(() => { | |
console.error('Failed to refresh token'); | |
}); | |
}, 60000) | |
}).error(() => { | |
console.error("Authenticated Failed"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment