Components · Reference
KrakstackAuthProvider
Share auth session state and project configuration in React and Effect UI HTML.
Preview
Loading preview…
Usage
import {
AuthProviderMessage,
KrakstackAuthProvider,
useAuth,
useSession,
} from "@krak-stack/auth/auth-provider/react";
import { AsyncResult } from "effect/reactivity";
const Account = () => {
const result = useSession();
const locale = useAuth()?.locale ?? "en";
const copy =
locale === "fr"
? {
loading: "Chargement de la session…",
error: "Session indisponible.",
guest: "Non connecté.",
}
: {
loading: "Loading session…",
error: "Session unavailable.",
guest: "Signed out.",
};
if (AsyncResult.isInitial(result)) return <p role="status">{copy.loading}</p>;
if (AsyncResult.isFailure(result)) return <p role="alert">{copy.error}</p>;
return <p>{result.value?.user.email ?? copy.guest}</p>;
};
export const Example = () => (
<KrakstackAuthProvider loadConfig={false}>
<Account />
</KrakstackAuthProvider>
);
// Opt in to loading organizations and refresh them along with the session.
const ProjectAccount = () => {
const auth = useAuth();
if (!auth) return null;
const organizations = AsyncResult.getOrElse(
auth.model.organizations,
() => [],
);
return (
<section>
<h2>{auth.projectConfig?.name}</h2>
<Account />
<ul>
{organizations.map((organization) => (
<li key={organization.id}>{organization.name}</li>
))}
</ul>
<button
type="button"
disabled={
auth.sessionResult.waiting || auth.model.organizations.waiting
}
onClick={() => auth.send(AuthProviderMessage.RefreshSession())}
>
{auth.locale === "fr" ? "Actualiser le compte" : "Refresh account"}
</button>
</section>
);
};
export const ProjectExample = () => (
<KrakstackAuthProvider projectId="your-project-id" loadOrganizations>
<ProjectAccount />
</KrakstackAuthProvider>
);
Wrap the routes that use the SDK with KrakstackAuthProvider. In React, useAuth() reads the provider’s Effect UI model and send, project configuration, and locale. Use useSession() to read the current session result with or without a provider. Effect UI HTML descendants read AuthProviderState; apply provideAuth to the hosting component.
The usage examples show a session-only setup (loadConfig: false) and a project setup that opts in to loadOrganizations. Sending AuthProviderMessage.RefreshSession() refreshes the session and enabled organization queries. It does not sign the user in or out.
See React components and CSS for Tailwind setup, locale syncing, and the provider hooks.