1.0.9 • Published 9 months ago

react-keycloak-provider v1.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

React Keycloak Provider

tests npm

Based on keycloak-js package, wraps it with React Context Provider

usage
// providing
import { KeycloakProvider } from 'react-keycloak-provider';

const root = ReactDOM.createRoot(
  document.getElementById('root') as HTMLElement
);
root.render(
  <React.StrictMode>
    <KeycloakProvider
      // config={window.location.origin + '/keycloak.json'}
        
      // passed to constructor
      // const keycloak = new Keycloak({ ... })  
      config={{
         url: 'http://localhost:8282/',
         realm: 'demo',
         clientId: 'react-client'
      }}
        
      // passed to keycloak.init({ ... }) 
      initOptions={{
        // 'check-sso' or 'login-required'
        onLoad: 'check-sso',    
        // if  'check-sso' copy 'silent-check-sso.html' to /public folder
        silentCheckSsoRedirectUri: window.location.origin + '/silent-check-sso.html', 
      }}
    >
      <App />
    </KeycloakProvider>
  </React.StrictMode>
);
// consuming
import { useKeycloakContext } from 'react-keycloak-provider';
import Button from '@mui/material/Button';

const AppBar = ()=> {
    const {keycloak, authenticated} = useKeycloakContext();
    return (
        <div> 
            {authenticated? 
                <Button onClick={keycloak.logout} label="Logout" /> :  
                <Button onClick={keycloak.login} label="Login" />
            } 
        </div>
    )

};
<!-- silent-check-sso.html  -->
<html>
    <body>
        <script>
            parent.postMessage(location.href, location.origin)
        </script>
    </body>
</html>

References

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago

1.0.9

9 months ago

1.0.8

9 months ago

1.0.7

9 months ago

1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

0.0.8

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago