Skip to content

Control components

Control components decide what to render or where to send the visitor. They come from @zautha/react and @zautha/nextjs and need a <ZauthaProvider> above them.

Renders children when the condition holds, else fallback. It renders nothing until Zautha has loaded, so a signed-in user never sees a signed-out flash. when is "signed-in", "signed-out", a check ({ role } or { permission }, full keys such as org:admin or org:invoices:read) or a function that gets has.

import { Show } from "@zautha/react";
export function Invoices() {
return (
<>
<Show when="signed-out">
<p>Sign in to continue.</p>
</Show>
<Show when={{ permission: "org:invoices:read" }} fallback={<p>No access</p>}>
<p>Invoices</p>
</Show>
<Show when={(has) => has({ role: "org:admin" }) || has({ permission: "org:invoices:manage" })}>
<button>Edit</button>
</Show>
</>
);
}

<Show> only hides UI: check the same permission on your server.

ZauthaLoading, ZauthaLoaded, ZauthaDegraded and ZauthaFailed

Section titled “ZauthaLoading, ZauthaLoaded, ZauthaDegraded and ZauthaFailed”

Render their children for one state of the SDK. ZauthaLoading is the time before the first state is known; ZauthaLoaded is a loaded SDK, including a degraded one; ZauthaDegraded is network trouble after the load (the last known state is shown meanwhile); ZauthaFailed is an SDK that could not load.

import { ZauthaDegraded, ZauthaFailed, ZauthaLoaded, ZauthaLoading } from "@zautha/react";
export function Gate({ children }: { children: React.ReactNode }) {
return (
<>
<ZauthaLoading>
<p>Loading…</p>
</ZauthaLoading>
<ZauthaFailed>
<p>Sign-in is unavailable. Reload the page.</p>
</ZauthaFailed>
<ZauthaDegraded>
<p>Offline: showing what we last knew.</p>
</ZauthaDegraded>
<ZauthaLoaded>{children}</ZauthaLoaded>
</>
);
}

Send a signed-out visitor to sign-in (or sign-up) once Zautha has loaded, and render nothing. redirectUrl is where to return to afterwards.

import { RedirectToSignIn, Show } from "@zautha/react";
export function Private({ children }: { children: React.ReactNode }) {
return (
<>
<Show when="signed-out">
<RedirectToSignIn redirectUrl="/dashboard" />
</Show>
<Show when="signed-in">{children}</Show>
</>
);
}

<RedirectToSignUp redirectUrl="…" /> does the same for sign-up. They use the sign-in and sign-up URLs the SDK is configured with, by default your instance’s hosted pages.

SignInButton, SignUpButton and SignOutButton

Section titled “SignInButton, SignUpButton and SignOutButton”

Unstyled triggers. They wrap their single child element (or render a plain <button> with a default label) and add the click behaviour; a click the child’s own handler cancels (preventDefault) does nothing.

import { SignInButton, SignOutButton, SignUpButton } from "@zautha/react";
export function Buttons() {
return (
<>
<SignInButton mode="modal" redirectUrl="/dashboard">
<button className="btn">Log in</button>
</SignInButton>
<SignUpButton />
<SignOutButton redirectUrl="/">Leave</SignOutButton>
</>
);
}
Prop On Meaning
mode SignInButton, SignUpButton redirect (default) goes to the sign-in or sign-up page; modal opens the prebuilt component (needs ui).
redirectUrl all three Where to go afterwards. SignOutButton: by default the provider’s afterSignOutUrl, by default /; null stays.
children all three The element to wire, or the button’s content.

SignOutButton has no error surface: when the first load could not read the client, signing out is refused and nothing happens. To tell the user, call useAuth().signOut() yourself and handle its rejection (see signOut).

The consent screen of an OIDC client: it shows who asks for what, then posts the decision and follows the redirect (see OpenID Connect). Put it on the page your consent URL points to and hand it the authorize query string.

import { OAuthConsent } from "@zautha/react";
export function ConsentPage() {
return <OAuthConsent authorizeQuery={window.location.search.slice(1)} />;
}
Prop Meaning
authorizeQuery The original authorize query string (the consent page’s own location.search without the ?).
navigate Follows the decision’s redirect; defaults to a full-page navigation.
onError Called with the ZauthaError of a request that failed.
onEvent Receives the component’s UiEvents.

@zautha/nextjs renders ZauthaClientProvider from its server <ZauthaProvider>: it is the client boundary that hands the keys (and, with dynamic, the request’s initial state) to the React provider (with ui already set). It is exported only so the server entry can reach it; render <ZauthaProvider> in your root layout instead (see @zautha/nextjs).

import { ZauthaProvider } from "@zautha/nextjs";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<ZauthaProvider>{children}</ZauthaProvider>
</body>
</html>
);
}