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 🙏

© 2026 – Pkg Stats / Ryan Hefner

keycloak-provider

v1.1.0

Published

A modern React provider and hook for seamless Keycloak integration with built-in RBAC support.

Downloads

777

Readme

Keycloak Provider for React

A lightweight, modern React library for integrating Keycloak authentication and Role-Based Access Control (RBAC) into your applications.

Features

  • Modern Hooks: Use useKeycloak() to access authentication state anywhere.
  • Functional Components: Built with React Hooks for better performance and smaller bundle size.
  • Protected Routes: Easily wrap your routes with <ProtectedRoute /> to enforce authentication.
  • Automatic Token Refresh: Seamlessly handles silent token refreshes in the background.
  • Built-in RBAC Support: Simple helpers for realm and resource role checks.
  • TypeScript Ready: Full type definitions included for a better developer experience.

Installation

npm install keycloak-provider

Quick Start

1. Setup the Provider

Wrap your application with KeycloakAuthProvider at the top level.

import React from 'react';
import { KeycloakAuthProvider } from 'keycloak-provider';

const keycloakConfig = {
  url: 'https://your-keycloak-server.com/auth',
  realm: 'your-realm',
  clientId: 'your-client-id'
};

function App() {
  return (
    <KeycloakAuthProvider keycloakConfig={keycloakConfig}>
      <Main />
    </KeycloakAuthProvider>
  );
}

2. Using the useKeycloak Hook

Access the Keycloak instance and authentication status in any functional component.

import { useKeycloak } from 'keycloak-provider';

function Profile() {
  const { keycloak, authenticated } = useKeycloak();

  if (!authenticated) return <div>Not logged in</div>;

  return (
    <div>
      <h1>Welcome, {keycloak.tokenParsed.preferred_username}</h1>
      <button onClick={() => keycloak.logout()}>Logout</button>
    </div>
  );
}

3. Protecting Routes

Use the ProtectedRoute component to restrict access to specific parts of your app.

import { ProtectedRoute } from 'keycloak-provider';

function Dashboard() {
  return (
    <ProtectedRoute>
      <div>This is a secret dashboard!</div>
    </ProtectedRoute>
  );
}

4. Role-Based Access Control (RBAC)

Check for roles using built-in helpers.

import { useKeycloak, hasRealmRole } from 'keycloak-provider';

function AdminPanel() {
  const { keycloak } = useKeycloak();

  if (!hasRealmRole(keycloak, 'admin')) {
    return <div>Access Denied</div>;
  }

  return <div>Welcome, Administrator</div>;
}

API Reference

KeycloakAuthProvider

  • keycloakConfig: Object containing your Keycloak configuration.
  • initOptions: (Optional) Options passed to keycloak.init().
  • loadingComponent: (Optional) Custom component to show while Keycloak is initializing.
  • onTokens: (Optional) Callback triggered when tokens are received or refreshed.

useKeycloak()

Returns an object:

  • keycloak: The Keycloak instance.
  • authenticated: Boolean status.
  • initialized: Boolean indicating if initialization is complete.

License

ISC