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

# Quickstart

> Add Login with Aiveth to a web app with the authorization code flow and PKCE.

This guide adds a "Login with Aiveth" button to a web app that has its own server. You register the app in the Aiveth console, send people to Aiveth to sign in, exchange the code you get back for tokens, and read who signed in.

Aiveth uses the OpenID Connect authorization code flow. PKCE (with `S256`) is required for every app, including apps that have a secret.

## Before you start

* You can open the Aiveth console and you have the power to manage apps (the **Apps** page is in your menu).
* While you build, use your [Development environment](https://docs.aiveth.com/environments.md). It allows `http://localhost` return addresses. A production app accepts https addresses only.

## 1. Register the app

**1. Open Apps**

In the console, open **Apps** and select **Register app**.

**2. Describe the app**

Enter a **Name**. Under **Kind**, choose **Server app** (a web app with its own server that keeps a secret).

**3. Add a return address**

Under **Return addresses**, add the address Aiveth sends people back to, one per line. For example `http://localhost:3000/callback`. Aiveth only returns to these exact addresses.

**4. Save the credentials**

Select **Register app**. Copy the **Client ID** and **Client secret**, then select **I've saved them**. The secret is not shown again.

The same screen shows the **Token address** and the **Discovery address**. The discovery document at `https://accounts.aiveth.com/.well-known/openid-configuration` lists every endpoint, so most libraries need only that address.

## 2. Send the person to Aiveth

Make a random `state`, a random `nonce`, and a PKCE `code_verifier` (43 to 128 characters). Keep all three in the person's session on your server. Then redirect the browser to the authorization endpoint:

```text theme={null}
https://accounts.aiveth.com/oauth/authorize
  ?response_type=code
  &client_id=YOUR_CLIENT_ID
  &redirect_uri=http://localhost:3000/callback
  &scope=openid email profile
  &state=STATE
  &nonce=NONCE
  &code_challenge=BASE64URL(SHA256(code_verifier))
  &code_challenge_method=S256
```

The person signs in on Aiveth's page, if they have not already. Aiveth then redirects to your `redirect_uri` with `code`, `state` and `iss`:

```text theme={null}
http://localhost:3000/callback?code=...&state=STATE&iss=https://accounts.aiveth.com
```

Check that `state` matches the one you stored before you use the code. If something went wrong, the redirect carries `error` and `error_description` instead.

## 3. Exchange the code for tokens

From your server, post the code to the token endpoint within two minutes. A code works once.

```bash curl theme={null}
curl -X POST https://accounts.aiveth.com/oauth/token \
  -u "YOUR_CLIENT_ID:YOUR_CLIENT_SECRET" \
  -d grant_type=authorization_code \
  -d code=THE_CODE \
  -d redirect_uri=http://localhost:3000/callback \
  -d code_verifier=THE_CODE_VERIFIER
```

```js Node (fetch) 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',
    code,
    redirect_uri: 'http://localhost:3000/callback',
    code_verifier: session.codeVerifier,
    client_id: process.env.AIVETH_CLIENT_ID,
    client_secret: process.env.AIVETH_CLIENT_SECRET,
  }),
});
const tokens = await res.json();
```

A successful answer looks like this:

```json theme={null}
{
  "access_token": "...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "openid email profile",
  "id_token": "eyJhbGciOiJSUzI1NiIs..."
}
```

You also get a `refresh_token` when you asked for the `offline_access` scope. See [Tokens and claims](https://docs.aiveth.com/apps/tokens-and-claims.md).

## 4. Read who signed in

The ID token is a JWT signed with RS256. Verify it against Aiveth's published keys, check that `aud` is your client ID, and check that `nonce` matches the one you stored.

> [tab] Node SDK
>
> Install the SDK:
>
> ```bash theme={null}
> npm install @gooclaim/aiveth
> ```
>
> ```ts theme={null}
> import { Aiveth } from '@gooclaim/aiveth';
>
> const aiveth = new Aiveth({ baseUrl: 'https://accounts.aiveth.com' });
>
> // Checked offline against Aiveth's keys (fetched once and cached)
> const claims = await aiveth.tokens.verifyIdToken(tokens.id_token, {
>   clientId: process.env.AIVETH_CLIENT_ID!,
> });
> if (claims.nonce !== session.nonce) throw new Error('nonce mismatch');
>
> console.log(claims.sub, claims.email, claims.org_id);
>
> // The access token is opaque: Aiveth answers who it stands for (null once it has ended)
> const user = await aiveth.tokens.userFromAccessToken(tokens.access_token);
> ```

> [tab] HTTP
>
> Fetch the keys from the `jwks_uri` in the discovery document (`https://accounts.aiveth.com/v1/auth/.well-known/jwks.json`) and verify the ID token with any JWT library.
>
> To read the person's details with the access token, call userinfo:
>
> ```bash theme={null}
> curl https://accounts.aiveth.com/oauth/userinfo \
>   -H "Authorization: Bearer ACCESS_TOKEN"
> ```
>
> ```json theme={null}
> {
>   "sub": "6f1c2b9e-...",
>   "org_id": "1a2b3c4d-...",
>   "email": "asha@acme.com",
>   "email_verified": true,
>   "name": "Asha Rao",
>   "given_name": "Asha",
>   "family_name": "Rao"
> }
> ```

Use `sub` as the person's stable ID in your app. Create your own session for them, and you are done.

## Next steps

* Ask for the person's role and app permissions with the `roles` scope. See [Tokens and claims](https://docs.aiveth.com/apps/tokens-and-claims.md).
* Building a single-page app instead? See [Single-page apps](https://docs.aiveth.com/apps/single-page-apps.md).
* Sign people out of Aiveth from your app. See [Logout](https://docs.aiveth.com/apps/logout.md).
* See every endpoint and parameter in [OpenID Connect endpoints](https://docs.aiveth.com/api-reference/oidc-endpoints.md).


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