Created
May 18, 2019 05:08
-
-
Save akoserwal/c3ba42405dd8cc5eb3f72aefdaa3be40 to your computer and use it in GitHub Desktop.
react-app-with-keycloak
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: 'react-test-app', onLoad: 'login-required' | |
} | |
let keycloak = Keycloak(initOptions); | |
keycloak.init({ onLoad: initOptions.onLoad }).success((auth) => { | |
if (!auth) { | |
window.location.reload(); | |
} else { | |
console.info("Authenticated"); | |
} | |
//React Render | |
ReactDOM.render(<App />, document.getElementById('root')); | |
localStorage.setItem("react-token", keycloak.token); | |
localStorage.setItem("react-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