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

@guardhouse/react

v1.0.0

Published

Frontend SDK for React 18+ with hooks and protected routes

Readme

@guardhouse/react

React SDK for adding Guardhouse/OIDC authentication to React 18+ browser apps.

Installation

npm install @guardhouse/react

Example Project

What You Get

  • GuardhouseProvider for auth state and callback handling
  • useAuth() hook for login, logout, user state, and token access
  • ProtectedRoute and withAuthenticationRequired route guards
  • Session persistence in sessionStorage (not localStorage)

Quick Start

1) Wrap your app with GuardhouseProvider

import { GuardhouseProvider } from "@guardhouse/react";
import { BrowserRouter } from "react-router-dom";

export function Root() {
  return (
    <GuardhouseProvider
      config={{
        authority: "https://auth.example.com",
        clientId: "your-client-id",
        audience: "https://api.example.com",
        redirectUri: `${window.location.origin}/callback`,
        logoutRedirectUri: `${window.location.origin}/callback`,
      }}
    >
      <BrowserRouter>{/* your routes */}</BrowserRouter>
    </GuardhouseProvider>
  );
}

2) Use useAuth() in your UI

import { useAuth } from "@guardhouse/react";

export function AccountPanel() {
  const { isLoading, isAuthenticated, user, loginWithRedirect, logout } =
    useAuth();

  if (isLoading) {
    return <div>Loading...</div>;
  }

  if (!isAuthenticated) {
    return <button onClick={() => void loginWithRedirect()}>Sign in</button>;
  }

  return (
    <div>
      <div>Signed in as {user?.name ?? user?.sub}</div>
      <button onClick={() => void logout()}>Sign out</button>
    </div>
  );
}

Route Guards

ProtectedRoute

import { ProtectedRoute } from "@guardhouse/react";

<Route
  path="/settings"
  element={
    <ProtectedRoute onRedirecting={() => <div>Redirecting...</div>}>
      <SettingsPage />
    </ProtectedRoute>
  }
/>;

withAuthenticationRequired

import { withAuthenticationRequired } from "@guardhouse/react";

type BillingProps = {
  orgId: string;
};

function BillingPage({ orgId }: BillingProps) {
  return <div>Billing for {orgId}</div>;
}

export const ProtectedBillingPage = withAuthenticationRequired<BillingProps>(
  BillingPage,
  {
    returnTo: "/billing",
    onRedirecting: () => <div>Redirecting...</div>,
  },
);

Both guards include React 18 Strict Mode safeguards to avoid duplicate login redirects.

useAuth() API

useAuth() returns:

  • isLoading: boolean
  • isAuthenticated: boolean
  • user: OIDC user profile or null
  • error: auth error message or null
  • loginWithRedirect(options?): starts login redirect
  • logout(options?): starts logout redirect
  • getAccessToken(): returns a valid access token or null
  • getAccessTokenSilently(): silent token retrieval/refresh or null

Provider Configuration (Most Used)

| Option | Required | Purpose | | ----------------------------------- | -------- | ----------------------------------------------- | | authority | Yes | OIDC issuer base URL | | clientId | Yes | OAuth/OIDC client ID | | redirectUri | Yes | Callback URI registered at your IdP | | audience | Usually | API audience for code flow | | logoutRedirectUri | No | Post-logout redirect URI | | scope | No | Defaults to openid profile email | | onRedirectCallback | No | Receives optional appState after callback | | allowAuthorizationWithoutAudience | No | Set true only for IdPs that reject audience | | allowOfflineAccessScope | No | Required if requesting offline_access | | debug | No | Enables SDK debug logs |

For advanced options (timeouts, endpoint overrides, DPoP, hardening flags), use the fields available in GuardhouseConfig.

SSR / Next.js / Remix

  • GuardhouseProvider and useAuth() are client-side APIs (they use browser storage/location).
  • Place auth components behind a client boundary ("use client" in Next.js).
  • First render starts with isLoading: true, then auth state is restored on the client.
  • Always branch on isLoading before rendering auth-required UI.
"use client";

import { useAuth } from "@guardhouse/react";

export function AuthGate() {
  const { isLoading, isAuthenticated } = useAuth();

  if (isLoading) {
    return <div>Loading...</div>;
  }

  return isAuthenticated ? <Dashboard /> : <LoginScreen />;
}

Security and Runtime Notes

  • Tokens/session are stored only in sessionStorage under gh_oidc_session.
  • The SDK does not use localStorage for auth session persistence.
  • Callback handling uses @guardhouse/core validation and secure JWT decoding.
  • Concurrent refresh requests are deduplicated to avoid refresh-token rotation races.
  • If your app reads id_token, validate signature/claims with a proper JWT/OIDC validation library before trusting claims.

Integration Checklist

  • Register exact redirectUri and logoutRedirectUri in your IdP client settings.
  • If using code flow and your IdP requires it, set a valid audience.
  • If requesting offline_access, set allowOfflineAccessScope: true.
  • Ensure protected pages handle isLoading and unauthenticated states explicitly.

License

See the repository LICENSE file.