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

react-router-keycloak

v1.3.1

Published

Integrate the Identity Server Component inside a react app

Downloads

222

Readme

react-router-keycloak

React components to integrate the Identity Service Component based on KeyCloak.

Installation

npm install --save react-router-keycloak

Usage

You'll need to wrap your application in a KeycloakProvider which will provide the right context to the other components.

Then use the Login, Logout and PrivateRoute anywhere as you would do with a react-router route.

You can pass a child to the Login component to display a custom loading if necessary.

The module consists of:

  • configureKeycloak: instantiate the keycloak library with the right options
    • keycloakUrl: (required) The url to your keycloak server
    • realm: (required) The keycloak realm to use
    • clientId: (required) The keycloak client_id to use
  • KeycloakProvider: provide keycloak context to the components
    • loginPath: (required) A react-router path to the Login component
    • logoutPath: (required) A react-router path to the Logout component
    • onRefresh: (required) Called every time the token is refreshed so you can update it locally
    • refreshRate: (optional) An interval expressed in seconds before trying to refresh the token. Default set to 10 seconds
    • minValidity: (optional) If the token expires within MIN_VALIDITY seconds, the token is refreshed. Default set to 30 seconds
  • Login: authenticate via Keycloak and start a new session
    • redirectTo: (required)
    • onSuccess: (optional) Called after a successful login
    • onFailure: (optional) Called after a login failure
    • children: (optional) Used to display during loading
  • Logout: terminate an ongoing session
    • redirectTo: (required) A react-router path to a public component
    • onSuccess: (optional) Called after a successful logout
    • children: (optional) Used to display during loading
  • PrivateRoute: check the user token for private routes and refreshes the token if necessary
    • path: (required) A react-router path to render you component
    • component: (required) The component you want to make private

Example

import React from 'react';
import Router, { Route } from 'react-router';
import KeycloakProvider, { configureKeycloak, PrivateRoute, Login, Logout } from 'react-router-keycloak';

const KEYCLOAK_URL = 'The `url` to your keycloak server';
const KEYCLOAK_REALM = 'The keycloak `realm` to use';
const KEYCLOAK_CLIENT_ID = 'The keycloak `client_id` to use';

function handleRefresh(token) {
  console.log('Called every time the token is refreshed so you can update it locally', token);
}

// Initialize a keycloak instance that will be used in every sub-components
configureKeycloak(KEYCLOAK_URL, KEYCLOAK_REALM, KEYCLOAK_CLIENT_ID);

export default () => (
  <KeycloakProvider loginPath="/login" logoutPath="/logout" onRefresh={handleRefresh}>
    <Router>
      <Route exact path="/" render={() => <div>A public home page</div>} />
      <Route path="/login" render={props => <Login redirectTo="/private" {...props} />} />
      <Route path="/log-out" render={props => <Logout redirectTo="/" {...props} />} />
      <PrivateRoute path="/private" component={() => <div>A private page</div>} />
    </Router>
  </KeycloakProvider>
);

Contribution

Create a pull request for every change. Make sure unit tests are written and working, run lint and rebuild your files.