> ## Documentation Index
> Fetch the complete documentation index at: https://docs.aiveth.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Single-page apps

> Sign people in from a browser app with PKCE, and let it call Aiveth's token endpoint directly.

A single-page app runs entirely in the browser, so it can't keep a secret. It signs people in with the authorization code flow and PKCE, and calls Aiveth's token endpoint itself. Aiveth allows those browser calls only from the web origins of apps you registered.

## Register a browser app

**1. Register the app**

In the console, open **Apps** → **Register app**. Under **Kind**, choose **Browser or mobile app**.

**2. Add your callback page**

Under **Return addresses**, add your callback page, for example `https://app.acme.com/callback`. In your [Development environment](https://docs.aiveth.com/environments.md) you can use `http://localhost:5173/callback`.

**3. Copy the client ID**

A browser app has no secret. The credentials screen says: "No secret: this app proves itself with PKCE on every sign-in."

## Registered web origins

Aiveth answers cross-origin (CORS) requests from a browser only when all of these are true:

* **The path is one of these:** `/oauth/token`, `/oauth/userinfo`, `/oauth/revoke`, `/.well-known/openid-configuration` and `/v1/auth/.well-known/jwks.json`. Never the sign-in page or any other API.
* **The origin belongs to a public app.** The origin is the scheme, host and port of one of the return addresses of an active app **without** a secret. A server app's return addresses don't count; a server app never needs the browser to call Aiveth.
* **No credentials.** Aiveth never sends `Access-Control-Allow-Credentials`, so the browser never sends Aiveth's cookies along. Each call carries its own code or token.

The allowed methods are `GET` and `POST`, and the allowed request headers are `Authorization`, `Content-Type` and `DPoP`. Preflight answers may be cached for 10 minutes. A request from any other origin gets no CORS headers at all, so the browser blocks it.

There is no separate "allowed origins" list to fill in: the return addresses are the list. A new app's origin works within about a minute.

## Use the React SDK

The React SDK runs the whole flow for you.

```bash theme={null}
npm install @gooclaim/aiveth-react
```

```tsx theme={null}
import { AivethProvider, SignedIn, SignedOut, SignInButton, UserButton, useAiveth } from '@gooclaim/aiveth-react';

export function App() {
  return (
    <AivethProvider
      issuer="https://accounts.aiveth.com"
      clientId="YOUR_CLIENT_ID"
      redirectUri="https://app.acme.com/callback"
      postLogoutRedirectUri="https://app.acme.com/"
    >
      <SignedOut><SignInButton /></SignedOut>
      <SignedIn><UserButton /></SignedIn>
    </AivethProvider>
  );
}

// Anywhere inside the provider
const { user, getAccessToken } = useAiveth();
const token = await getAccessToken();
```

* Sign-in happens on Aiveth's page, with code and PKCE.
* Tokens are kept in memory, never in `localStorage`. After a reload, `signIn()` comes straight back while the person is still signed in at Aiveth.
* The SDK asks for `offline_access` by default and swaps the refresh token at every use.
* An app belongs to one organization, so there is no organization switcher.

## Without the SDK

Any OpenID Connect library for browsers works. The token request from a public app sends `client_id` in the form body and no `client_secret`:

```js theme={null}
const res = await fetch('https://accounts.aiveth.com/oauth/token', {
  method: 'POST',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  body: new URLSearchParams({
    grant_type: 'authorization_code',
    client_id: 'YOUR_CLIENT_ID',
    code,
    redirect_uri: 'https://app.acme.com/callback',
    code_verifier,
  }),
});
```

A public app must not send a secret. If it does, Aiveth answers `invalid_client`.

## Security notes

* Keep tokens in memory. Don't put them in `localStorage` or `sessionStorage`.
* Always check `state` on the callback, and `nonce` in the ID token.
* Consider [DPoP](https://docs.aiveth.com/apps/dpop.md): it binds the tokens to a key held by the browser, so a copied token is useless. A public app's refresh tokens are bound too.

## Related

* [Login with Aiveth](https://docs.aiveth.com/apps/login-with-aiveth.md)
* [Tokens and claims](https://docs.aiveth.com/apps/tokens-and-claims.md)
* [React SDK](https://docs.aiveth.com/sdks/react.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.