Created
June 7, 2018 17:42
-
-
Save kmikulski/45b01d9e281752040c2b54464eaa1c63 to your computer and use it in GitHub Desktop.
the description for this gist
This file contains 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
import React, { Component } from 'react'; | |
import Keycloak from 'keycloak-js'; | |
class Secured extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { keycloak: null, authenticated: false }; | |
} | |
componentDidMount() { | |
const keycloak = Keycloak('/keycloak.json'); | |
keycloak.init({onLoad: 'login-required'}).then(authenticated => { | |
this.setState({ keycloak: keycloak, authenticated: authenticated }) | |
}) | |
} | |
render() { | |
if (this.state.keycloak) { | |
if (this.state.authenticated) return ( | |
<div> | |
<p>This is a Keycloak-secured component of your application. You shouldn't be able | |
to see this unless you've authenticated with Keycloak.</p> | |
</div> | |
); else return (<div>Unable to authenticate!</div>) | |
} | |
return ( | |
<div>Initializing Keycloak...</div> | |
); | |
} | |
} | |
export default Secured; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment