Documentation / v1 / Quickstarts

React and browser apps

Public OAuth with a short-lived, single-use PKCE transaction. No client secret in the browser.

Start authorization

Use the browser export. Generate a new verifier and state for every attempt. Persist only the short-lived transaction needed after the redirect; keep access tokens in memory or use an HttpOnly-cookie backend session.

React / TypeScript · typescript
import { BrowserAuthClient, browserGenerateCodeVerifier,
  browserGenerateCodeChallengeAsync } from "@beyondigital/auth-sdk/browser";

const issuer = "https://auth.example.com";
const redirectUri = location.origin + "/auth/callback";
const client = new BrowserAuthClient({ issuerUrl: issuer,
  clientId: "your-public-client", redirectUri });
const verifier = browserGenerateCodeVerifier();
const state = crypto.randomUUID();
sessionStorage.setItem("auth-transaction", JSON.stringify({
  verifier, state, createdAt: Date.now()
}));
location.assign(client.createAuthorizationUrl({
  state, resource: "https://api.example.com",
  codeChallenge: await browserGenerateCodeChallengeAsync(verifier)
}));

Validate the callback before exchanging

Run this on the registered callback page. The validator checks the exact callback, state, issuer and duplicate response parameters. Clear the transaction before exchanging the code so a callback cannot be replayed. Use the same resource at authorization and token exchange.

Callback · typescript
import { BrowserAuthClient, validateAuthorizationResponse } from "@beyondigital/auth-sdk/browser";

const issuer = "https://auth.example.com";
const redirectUri = location.origin + "/auth/callback";
const client = new BrowserAuthClient({ issuerUrl: issuer,
  clientId: "your-public-client", redirectUri });
const transaction = JSON.parse(sessionStorage.getItem("auth-transaction") ?? "null");
sessionStorage.removeItem("auth-transaction");
if (!transaction || typeof transaction.createdAt !== "number" ||
    transaction.createdAt > Date.now() ||
    Date.now() - transaction.createdAt > 300_000) {
  throw new Error("Sign-in expired. Start again.");
}
const { code } = validateAuthorizationResponse(location.href, {
  state: transaction.state, issuer, redirectUri
});
const tokens = await client.exchangeCode({ code,
  codeVerifier: transaction.verifier, resource: "https://api.example.com" });

Access tokens and identity are different

The recipe above requests API access. For OpenID Connect sign-in, request openid and a fresh nonce, then verify the ID token's signature, exact issuer, client audience, expiry and nonce before using it as identity. Decoding a JWT does not verify it. The browser SDK exposes low-level OAuth transport and callback validation; it is not a complete React session manager.