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

oidc-js-preact

v1.1.0

Published

Simple OIDC authentication for Preact. Hooks, provider, and auth guards with zero dependencies.

Readme

oidc-js-preact

Simple OIDC authentication for Preact. Drop-in hooks, provider, and auth guards with zero dependencies.

Part of the oidc-js family -- OpenID Connect for every JavaScript framework.

Install

npm install oidc-js-preact

preact (>=10) is a peer dependency.

Quick start

Wrap your app with AuthProvider:

import { AuthProvider } from "oidc-js-preact";

const config = {
  issuer: "https://auth.example.com",
  clientId: "my-app",
  redirectUri: "http://localhost:3000/callback",
  scopes: ["openid", "profile", "email"],
};

function App() {
  return (
    <AuthProvider config={config}>
      <Main />
    </AuthProvider>
  );
}

Use the useAuth hook in any child component:

import { useAuth } from "oidc-js-preact";

function Main() {
  const { isAuthenticated, isLoading, user, actions } = useAuth();

  if (isLoading) return <p>Loading...</p>;

  if (!isAuthenticated) {
    return <button onClick={() => actions.login()}>Log in</button>;
  }

  return (
    <div>
      <p>Welcome, {user?.profile?.name}</p>
      <button onClick={() => actions.logout()}>Log out</button>
    </div>
  );
}

RequireAuth

Guard a subtree so only authenticated users see it. Unauthenticated users are redirected to login automatically.

import { RequireAuth } from "oidc-js-preact";

function ProtectedPage() {
  return (
    <RequireAuth fallback={<p>Redirecting to login...</p>}>
      <Dashboard />
    </RequireAuth>
  );
}

RequireAuth will attempt a silent token refresh before redirecting. Disable this with autoRefresh={false}:

<RequireAuth autoRefresh={false}>
  <Dashboard />
</RequireAuth>

API reference

AuthProvider

Context provider that initializes the OIDC client and manages authentication state.

| Prop | Type | Default | Description | |------|------|---------|-------------| | config | OidcConfig | required | OIDC client configuration (issuer, clientId, redirectUri, scopes, etc.) | | fetchProfile | boolean | true | Fetch the userinfo endpoint after login to populate user.profile | | onLogin | (returnTo: string) => void | undefined | Called after a successful login redirect. Receives the original URL the user was on. If omitted, history.replaceState restores the URL automatically. | | onError | (error: Error) => void | undefined | Called when an authentication error occurs during initialization | | children | ComponentChildren | required | Child components |

useAuth()

Hook that returns the current AuthContextValue. Must be called inside an AuthProvider.

interface AuthContextValue {
  config: OidcConfig;
  user: AuthUser | null;
  isAuthenticated: boolean;
  isLoading: boolean;
  error: Error | null;
  tokens: AuthTokens;
  actions: AuthActions;
}

AuthUser

interface AuthUser {
  claims: IdTokenClaims;
  profile: OidcUser | null;
}

claims are decoded from the ID token. profile is populated from the userinfo endpoint when fetchProfile is enabled.

AuthTokens

interface AuthTokens {
  access: string | null;
  id: string | null;
  refresh: string | null;
  expiresAt: number | null;
}

AuthActions

interface AuthActions {
  login: (options?: LoginOptions) => void;
  logout: () => void;
  refresh: () => Promise<void>;
  fetchProfile: () => Promise<void>;
}

RequireAuth

Component that guards its children behind authentication. Renders fallback while loading or when the user is not authenticated.

| Prop | Type | Default | Description | |------|------|---------|-------------| | autoRefresh | boolean | true | Attempt a silent token refresh before redirecting to login | | loginOptions | LoginOptions | undefined | Options passed to login() when a redirect is triggered | | fallback | ComponentChildren | null | Rendered while loading or redirecting | | children | ComponentChildren | required | Content shown to authenticated users |

Documentation

Full documentation: https://eugenioenko.github.io/oidc-js/preact/auth-provider/

Repository

https://github.com/eugenioenko/oidc-js

License

MIT