Skip to content

Instantly share code, notes, and snippets.

@kmikulski
Created June 7, 2018 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmikulski/45b01d9e281752040c2b54464eaa1c63 to your computer and use it in GitHub Desktop.
Save kmikulski/45b01d9e281752040c2b54464eaa1c63 to your computer and use it in GitHub Desktop.
the description for this gist
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