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

@lgibbs/keycloak-auth-ts

v1.1.1

Published

Pragmatic Keycloak wrapper with SSR-safe init, single-flight refresh, fetchWithAuth, role helpers, and tiny pub/sub.

Downloads

11

Readme

@lgibbs/keycloak-auth-ts

A pragmatic TypeScript Keycloak wrapper with:

  • SSR-safe init
  • Queued / single-flight token refresh
  • fetchWithAuth with 401 retry
  • Role helpers
  • User profile convenience
  • Tiny pub/sub for UI state
  • Safe teardown

This wrapper keeps your app code tiny while handling the annoying parts of the Keycloak JS adapter.

Installation

npm install @lgibbs/keycloak-auth-ts keycloak-js

Note: keycloak-js is a peer dependency, so you must install it separately.

Quick Start

import { initAuth, fetchWithAuth, getAuthState, subscribe } from "@lgibbs/keycloak-auth-ts";

// Initialize once at app startup
await initAuth({
  url: "http://localhost:8180",
  realm: "myrealm",
  clientId: "my-client",
  onLoad: "check-sso",
  silentCheckUri: `${window.origin}/silent-check.html`
});

// Subscribe to auth state changes
const unsubscribe = subscribe((state) => {
  console.log("Auth state:", state);
});

// Fetch API with auth header (auto-refreshes token, retries once on 401)
const response = await fetchWithAuth("/api/data");
const data = await response.json();
console.log(data);

// Snapshot of current state (cheap read)
const state = getAuthState();
console.log("Current user:", state.profile?.username);

// Unsubscribe later if desired
unsubscribe();

API

initAuth(config: InitAuthConfig): Promise<void>

Initialize Keycloak and wire up refresh hooks. SSR-safe and idempotent.

getToken(opts?: { minValidity?: number }): Promise<string | null>

Retrieve a fresh token, ensuring at least minValidity seconds remain.

fetchWithAuth(input, init?, opts?): Promise<Response>

Fetch with Authorization header automatically. Retries once on 401.

subscribe(cb: (state: AuthState) => void): () => void

Subscribe to auth state changes. Returns an unsubscribe function.

getAuthState(): AuthState

Read-only snapshot of current auth state.

hasRealmRole(role: string): boolean

Check if user has a realm-level role.

hasResourceRole(role: string, clientId?: string): boolean

Check if user has a client/resource role.

getAuthHeader(): Promise<string | null>

Convenience function to return "Bearer <token>" or null.

getUserInfo(): Promise<object | null>

Fetch OIDC userinfo from Keycloak.

openAccount(): Promise<void>

Open Keycloak account management page.

login(options?): Promise<void> / logout(options?): Promise<void>

Login and logout helpers.

teardown(): void

Reset internal state and subscribers (e.g., in tests or app reboot).

withKeycloak(fn: (kc: KeycloakInstance) => T): T

Escape hatch to run a function with the raw Keycloak instance.

Development

Build locally:

npm install
npm run build

Test pack:

npm pack

License

MIT © Lauren Gibbs