Frontend
Vanilla browser
Use the framework-agnostic browser client and stores without React or Vue.
@circle-rd/auth-ai/client is the layer the React and Vue bindings are built on. It holds no token: it calls your backend's /auth routes with the session cookie, and exposes small observable stores to bind to any UI. Importing it pulls no server code.
Complete example
import {
createAuthStore,
createBrowserAuth,
createResourceStore,
formatEuros,
hasFeature,
} from "@circle-rd/auth-ai/client";
import type { AuthState } from "@circle-rd/auth-ai/client";
const auth = createBrowserAuth(); // basePath defaults to "/auth"
const authStore = createAuthStore(auth);
const wallet = createResourceStore(() => auth.getWallet(), {
pollIntervalMs: 15_000,
});
function render({ status, user, error }: AuthState): void {
const bar = document.getElementById("session");
if (!bar) return;
bar.replaceChildren();
const button = document.createElement("button");
if (status === "authenticated" && user) {
button.textContent = `Sign out ${user.name ?? user.email ?? user.sub}`;
button.addEventListener("click", () => void auth.logout());
bar.append(button, hasFeature(user, "beta") ? " (beta)" : "");
} else {
button.textContent = status === "loading" ? "Loading…" : "Sign in";
button.addEventListener("click", () => auth.login("/"));
bar.append(button, error ? ` ${error.code}` : "");
}
}
let started = false;
authStore.state.subscribe(() => {
const state = authStore.state.get();
render(state);
if (state.status === "authenticated" && !started) {
started = true;
wallet.start(); // loads now, then every 15 s
}
});
wallet.state.subscribe(() => {
const { data, error } = wallet.state.get();
const out = document.getElementById("wallet");
if (!out) return;
if (error) out.textContent = error.code;
else if (data) out.textContent = `${formatEuros(data.balance, 2)} € (canSpend: ${data.canSpend})`;
});
render(authStore.state.get());
void authStore.refresh();
How it works
createBrowserAuth()is the HTTP client:getUser(),login(next?),logout(),getWallet(),getWalletStats(days?),listModels().createAuthStore(auth)keeps anAuthStatein an observable and exposesrefresh(). Call it on page load, on focus, or after an API call of yours answered401.createResourceStore(load, { pollIntervalMs?, onError? })wraps any async loader in a{ status, data, error }state, withstart()(load and poll, returns the stop function) andrefresh().createObservable(initial)is the primitive:get(),set(value)andsubscribe(listener), which returns the unsubscribe function.
The pattern in the example (render on change, start polling once signed in) is the one used by the example app.
Calling your own API
Your own endpoints use the same cookie. Send same-origin requests with credentials: "same-origin", and handle a 401 by refreshing the auth store so the UI shows the sign-in:
import { createAuthStore, createBrowserAuth } from "@circle-rd/auth-ai/client";
const auth = createBrowserAuth();
const authStore = createAuthStore(auth);
export async function api(path: string, body?: unknown): Promise<unknown> {
const response = await fetch(path, {
method: body === undefined ? "GET" : "POST",
headers: body === undefined ? {} : { "content-type": "application/json" },
credentials: "same-origin",
...(body === undefined ? {} : { body: JSON.stringify(body) }),
});
if (response.status === 401) await authStore.refresh();
return response.json();
}
Browsers never see a token and your backend checks Origin on state-changing routes: see Security model.