npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@keycloak-react/web

v4.0.1

Published

React bindings for Keycloak javascript adapter

Downloads

19

Readme

React Keycloak

React Keycloak

React bindings for Keycloak

NPM (scoped)

License lerna Contributors Gitter

Dependencies Build Status Coverage Status Github Issues


Table of Contents


Install

React Keycloak requires:

  • React 16.0 or later
  • keycloak-js 9.0.2 or later
yarn add @keycloak-react/web

or

npm install --save @keycloak-react/web

or as a UMD package through unpkg

  • one for development: https://unpkg.com/@keycloak-react/web@latest/dist/umd/react-keycloak-web.js
  • one for production: https://unpkg.com/@keycloak-react/web@latest/dist/umd/react-keycloak-web.min.js

Support

| version | keycloak-js version | | ------- | ------------------- | | v2.0.0+ | 9.0.2+ | | v1.x | >=8.0.2 <9.0.2 |

Getting Started

Setup Keycloak instance

Create a keycloak.js file in the src folder of your project (where App.js is located) with the following content

import Keycloak from 'keycloak-js'

// Setup Keycloak instance as needed
// Pass initialization options as required or leave blank to load from 'keycloak.json'
const keycloak = new Keycloak()

export default keycloak

Setup KeycloakProvider

Wrap your App inside KeycloakProvider and pass the keycloak instance as prop

import { KeycloakProvider } from '@keycloak-react/web'

import keycloak from './keycloak'

// Wrap everything inside KeycloakProvider
const App = () => {
  return <KeycloakProvider keycloak={keycloak}>...</KeycloakProvider>
}

N.B. If your using other providers (such as react-redux) it is recommended to place them inside KeycloakProvider.

KeycloakProvider automatically invokes keycloak.init() method when needed and supports the following props:

  • initConfig, contains the object to be passed to keycloak.init() method, by default the following is used

    {
      onLoad: 'check-sso',
      promiseType: 'native',
    }

    for more options see Keycloak docs.

  • LoadingComponent, a component to be displayed while keycloak is being initialized, if not provided child components will be rendered immediately. Defaults to null

  • isLoadingCheck, an optional loading check function to customize LoadingComponent display condition. Return true to display LoadingComponent, false to hide it.

    Can be implemented as follow

    ;(keycloak) => !keycloak.authenticated
  • onEvent, an handler function that receives events launched by keycloak, defaults to null.

    It can be implemented as follow

    ;(event, error) => {
      console.log('onKeycloakEvent', event, error)
    }

    Published events are:

    • onReady
    • onAuthSuccess
    • onAuthError
    • onAuthRefreshSuccess
    • onAuthRefreshError
    • onTokenExpired
    • onAuthLogout
  • onTokens, an handler function that receives keycloak tokens as an object every time they change, defaults to null.

    Keycloak tokens are returned as follow

    {
      "idToken": string,
      "refreshToken": string,
      "token": string
    }

HOC Usage

When a component requires access to Keycloak, wrap it inside the withKeycloak HOC.

import { withKeycloak } from '@keycloak-react/web'

const LoginPage = ({ keycloak, keycloakInitialized }) => {
  // Here you can access all of keycloak methods and variables.
  // See https://www.keycloak.org/docs/latest/securing_apps/index.html#javascript-adapter-reference
  return (
    <div>
      <button type="button" onClick={() => keycloak.login()}>
        Login
      </button>
    </div>
  )
}

export default withKeycloak(LoginPage)

Hook Usage (React >=16.8 required)

Alternately, when a component requires access to Keycloak, you can also use the useKeycloak Hook.

import { useKeycloak } from '@keycloak-react/web'

export default () => {
  // Using array destructuring
  const [keycloak, initialized] = useKeycloak()
  // or Object destructuring
  const { keycloak, initialized } = useKeycloak()

  // Here you can access all of keycloak methods and variables.
  // See https://www.keycloak.org/docs/latest/securing_apps/index.html#javascript-adapter-reference

  return (
    <div>
      <div>{`User is ${
        !keycloak.authenticated ? 'NOT ' : ''
      }authenticated`}</div>

      {!!keycloak.authenticated && (
        <button type="button" onClick={() => keycloak.logout()}>
          Logout
        </button>
      )}
    </div>
  )
}

External Usage (Advanced)

If you need to access keycloak instance from non-React files (such as sagas, utils, providers ...), you can import the instance directly from the keycloak.js file.

The instance will be initialized by react-keycloak but you'll need to be carefull when using the instance and avoid setting/overriding any props, you can however freely access the exposed methods (such as refreshToken, login, etc...).

Examples

See inside examples for various demo implementing this library main features.

Note: The demo apps are not meant to be production-ready nor starter-kit s but just a way to show this module components and their usage.

Contributors

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!


If you found this project to be helpful, please consider buying me a coffee.

buy me a coffee