> ## 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.

# React

> Add Aiveth sign-in to a React single-page app with @gooclaim/aiveth-react.

`@gooclaim/aiveth-react` adds Aiveth by Gooclaim sign-in to a React single-page app. People sign in on Aiveth's own page with the authorization code flow and PKCE. Passwords, passkeys, two-step and single sign-on never pass through your app.

## Install

Requires React 18 or later.

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

## Set up the app in Aiveth

In the console, go to **Apps** and register a **public** app. Add your callback page as its return address, and your sign-out page under its sign-out addresses. The app's origin may then call Aiveth's token endpoint. See [Single-page apps](https://docs.aiveth.com/apps/single-page-apps.md).

Use your Development environment's app while you build (`http://localhost` is allowed there), and your production app (HTTPS only) when you ship.

## Add the provider

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

export function App() {
  return (
    <AivethProvider
      issuer="https://accounts.aiveth.com"
      clientId="your-app-client-id"
      redirectUri="https://app.example.com/callback"
      postLogoutRedirectUri="https://app.example.com/"
    >
      <SignedOut><SignInButton /></SignedOut>
      <SignedIn><UserButton /></SignedIn>
    </AivethProvider>
  );
}
```

On the return address, the provider finishes the sign-in by itself and cleans the code out of the address bar.

### `AivethProvider` props

**issuer** `string` *required*

Your Aiveth address. Must be HTTPS (HTTP only for localhost).

**clientId** `string` *required*

Your app's client ID.

**redirectUri** `string` *required*

Exactly one of the app's registered return addresses.

**scope** `string` (default: openid email profile offline\_access)

The scopes to ask for.

**postLogoutRedirectUri** `string`

Where Aiveth sends the person after signing out. Register it under the app's sign-out addresses.

**onSignedIn** `(returnTo: string | null) =\> void`

Called after a return from Aiveth, with where the app wanted to go. Use your router's navigate here.

## `useAiveth()`

Use it anywhere inside the provider.

```tsx theme={null}
import { useAiveth } from '@gooclaim/aiveth-react';

function Invoices() {
  const { isLoaded, isSignedIn, user, getAccessToken } = useAiveth();

  async function load() {
    const token = await getAccessToken();
    const res = await fetch('/api/invoices', { headers: { Authorization: `Bearer ${token}` } });
    // ...
  }

  if (!isLoaded) return null;
  if (!isSignedIn) return <p>Please sign in.</p>;
  return <h1>Hello, {user?.name}</h1>;
}
```

| Field | Type | Description |
| - | - | - |
| `isLoaded` | `boolean` | The provider has finished checking for a return from Aiveth. |
| `isSignedIn` | `boolean` | A person is signed in. |
| `user` | `User \| null` | The ID token's claims: `sub`, `org_id`, `email`, `name`, `role`, `permissions`, `picture` and more. |
| `error` | `Error \| null` | A sign-in that went wrong. |
| `signIn({ returnTo? })` | `Promise<void>` | Go to Aiveth's sign-in page. |
| `signOut()` | `Promise<void>` | Sign out at Aiveth and come back to `postLogoutRedirectUri`. |
| `getAccessToken()` | `Promise<string \| null>` | An access token for your API, renewed when needed. `null` when signed out. |
| `accountUrl` | `string` | The person's account page on Aiveth. |
| `hasFlag(key)` | `boolean` | Is this feature flag on for the person? |
| `actor` | `Actor \| null` | Who is acting as this person, and why. `null` otherwise. |
| `actingUrl` | `string` | Aiveth's page that ends acting as someone. |

On your server, check the access token with the [Node](https://docs.aiveth.com/sdks/node.md) or [Python](https://docs.aiveth.com/sdks/python.md) SDK's `userFromAccessToken` / `user_from_access_token`.

## Components

| Component | Shows |
| - | - |
| `<SignedIn>` | Its children only when a person is signed in. |
| `<SignedOut>` | Its children only when nobody is signed in. |
| `<SignInButton returnTo? className?>` | A button that starts sign-in. Default text "Sign in". |
| `<SignOutButton className?>` | A button that signs out. Default text "Sign out". |
| `<UserButton className?>` | The person's initials, opening a menu with their name and email, **Manage account** and **Sign out**. |
| `<ActingBanner className?>` | A line across the top while someone acts as this person. Nothing otherwise. |

## Acting for others

When a support person acts as someone, the ID token carries `act` with who and why. Show it, and block what support shouldn't do:

```tsx theme={null}
import { ActingBanner, useAiveth } from '@gooclaim/aiveth-react';

function Layout({ children }: { children: React.ReactNode }) {
  const { actor } = useAiveth();
  return (
    <>
      <ActingBanner />
      {children}
      {!actor && <PayNowButton />}
    </>
  );
}
```

## Feature flags and photos

```tsx theme={null}
const { user, hasFlag } = useAiveth();

{hasFlag('new-billing') && <NewBilling />}
<img src={user?.picture} alt="" />
```

`picture` is present with the `profile` scope.

## How tokens are kept

* Tokens are kept in memory, never in `localStorage`. After a reload, `signIn()` comes straight back while the person is still signed in at Aiveth.
* Refresh tokens (`offline_access`, asked by default) are swapped for new ones at every use.
* An access token close to its end is renewed before `getAccessToken()` returns it.
* One app belongs to one organization in Aiveth, so there is no organization switcher.

## Lower-level client

`AivethClient` is the class the provider uses. It has `signIn`, `handleRedirect`, `getAccessToken`, `signOut`, `accountUrl` and `actingUrl`, for apps that don't use the provider.

## Errors

| Error | When |
| - | - |
| `SignInError` | A sign-in that went wrong, or a bad `issuer` or `redirectUri`. |
| `RefusedError` | Aiveth said no. Its `code` is the OAuth error code. `invalid_grant` means the token has ended. |


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