Test mode: paid plans aren't on sale yet, so nothing is charged. Every plan's limits apply.

← Docs

@nod/react

Drop-in React components for signing in with Nod: a provider, sign-in and sign-out buttons, a user menu, an organization switcher, and hooks. Not published yet (publishing under a name is a person's step); use it from this repository until then. React 18 or 19, no other runtime dependency.

Sign-in stays on Nod's own page. <SignInButton> redirects the browser to your realm's /authorize; the person signs in there (passkey, email code, password, a company's SSO) and comes back with a code. Nothing here has a password field, so a page of yours can't be made to look like a sign-in and steal one, and passkeys keep working because they are bound to Nod's origin.

Quickstart

Register a public client whose redirect is a page of your app:

nod client register acme web --redirect https://app.example.com/callback --post-logout https://app.example.com/
import { NodProvider, SignInButton, SignedIn, SignedOut, UserButton, OrganizationSwitcher, useAuth } from "@nod/react";

export function Root() {
  return (
    <NodProvider issuer="https://auth.example.com/acme" clientId="web" redirectUri="https://app.example.com/callback"
                 postLogoutRedirectUri="https://app.example.com/">
      <header>
        <SignedOut><SignInButton /></SignedOut>
        <SignedIn><OrganizationSwitcher /><UserButton /></SignedIn>
      </header>
      <Orders />
    </NodProvider>
  );
}

function Orders() {
  const { getToken } = useAuth();
  // later: fetch("https://orders.example/orders", { headers: { authorization: `Bearer ${await getToken({ resource: "https://orders.example" })}` } })
}

Render <NodProvider> on the callback page too (the simplest is to render it at the app's root): it reads the code, exchanges it, and removes it from the address bar.

Components

<NodProvider issuer clientId redirectUri>Authorization code with PKCE. Also scope, resources, postLogoutRedirectUri, silentSignIn, organizationsUrl, listOrganizations, unstyled
<SignInButton organization? loginHint?>Redirects to Nod's page. Children are the label
<SignUpButton organization? loginHint?>The same redirect with prompt=create: Nod's page opens on creating an account (where the realm lets people sign up; otherwise on signing in). Children are the label
<SignOutButton>Revokes the refresh token, forgets everything, ends Nod's session
<UserButton>Avatar (or initials), name, and a menu: Manage account (Nod's account page), Sign out
<SignedIn> / <SignedOut>Render their children by state. <SignedOut> shows nothing while loading
<OrganizationSwitcher>The person's organizations (the accounts API's GET /orgs); choosing one signs in again with organization set, so tokens carry org_id
<PricingTable onSelect? ctaLabel? featureLabels? locale?>The realm's plans from Nod's public pricing (<issuer>/billing/pricing): price (Monthly/Yearly when plans offer both), features, the organization's current plan marked. onSelect(plan, price): start the checkout from your backend (it needs billing:write). No sign-in needed to show it
<OrganizationBilling featureLabels? locale? showPlans?>The organization's billing page for its own people: plan, status, renewal, features; for those who may manage it, "Manage billing" (Ducat's portal) and the plans to switch to. Needs ${issuer}/billing in resources and billing:view billing:manage in scope, and a role granting them in the organization (Nod's guide, "An organization's own billing page")
<Protect plan? feature? fallback?>Its children when the organization's plan is plan and/or includes feature (the token's org_plan, org_features); otherwise fallback. Nothing while loading. Hides UI only

Hooks

const { isLoaded, isSignedIn, user } = useUser();        // user: id, name, email, picture, claims…
const { getToken, signIn, signOut, organizationId } = useAuth();
const token = await getToken();                           // for Nod itself (userinfo)
const api = await getToken({ resource: "https://orders.example" });   // RFC 8707: aud is that API
const { organization, organizations, switchOrganization } = useOrganization({ list: true });
const { plan, features, has } = useBilling();             // has({ feature: "sso" }), has({ plan: "pro" })
const { billing, canManage, checkout, portal } = useOrganizationBilling();   // the organization's own billing
const { plans } = usePricing();                           // the public pricing; formatPrice(1200, "eur") is "€12.00"

useBilling reads org_plan and org_features from the newest token (Nod's guide, "Plans and what an organization paid for"): no org_plan is the free tier (plan null, has false). Each refresh of the app's own token updates it, so a plan change shows within a token's life without signing in again; a refreshed token for another person or organization is ignored. It decides what the page shows, never what's allowed: your API checks the token's claims.

getToken refreshes when the token is within 30 seconds of expiring, one refresh at a time (a rotated refresh token works once). It returns null when nobody is signed in or the session was ended. For getToken({ resource }) the API must be named in resources and its scopes in scope:

<NodProvider … scope="openid profile email offline_access orders:read" resources={["https://orders.example"]}>

Where tokens live

In memory, in the provider's client, and nowhere else: never localStorage or sessionStorage, where one XSS bug (or any script on the page) reads them for as long as they last. The cost is that a reload forgets them. So after a reload the provider makes one prompt=none round trip to Nod (it never shows a page): if the person's Nod session is still there, they're back in at once; if not, they're signed out and error stays empty. Only the PKCE verifier of a sign-in in progress, and a "was signed in" flag, are in sessionStorage; neither is a credential. Turn the round trip off with silentSignIn={false}.

Organizations

<OrganizationSwitcher> calls <issuer>/accounts/v1/orgs with the person's token, which Nod serves in its accounts realm (the Nod console). In another realm, pass listOrganizations={async () => [{ id, name }]} (your own call). The accounts API is its own API: list ${issuer}/accounts in resources so the call gets its own token.

Theming and accessibility

CSS variables, light and dark (the system's setting, or data-theme="dark" / "light" on <html>). Override any of --nod-accent, --nod-accent-contrast, --nod-bg, --nod-fg, --nod-muted, --nod-border, --nod-hover, --nod-focus, --nod-radius, --nod-font, --nod-shadow. The styles are one <style data-nod> the provider renders (unstyled leaves it out; the classes are nod-button, nod-trigger, nod-menu, nod-item). Menus are role="menu" with arrow keys, Home and End, Escape (focus goes back to the button), outside click, and a visible focus ring; the avatar is decorative and the button's name says whose menu it is.

Tests

npm ci && npm test          # typecheck, build, unit tests (vitest + Testing Library), and the end-to-end test
npm run test:unit
npm run test:e2e

The end-to-end test builds and starts the real nod-server binary on a throwaway PostgreSQL (KIT_PG_BIN, default ~/.local/opt/pg/bin; NOD_BIN or HARPORA_BIN for another binary), signs a person in on Nod's hosted endpoints with a password, and drives the components through the redirect, the callback, useUser, the organization claim, an API token, the CORS headers a browser would need, silent sign-in after a reload, the organizations list and sign-out. Not covered: a real browser rendering Nod's hosted page, and passkeys (jsdom has no WebAuthn).