# Get started with Customer Identity

> Add a customer, connect your app, and watch the customer allow it on the consent page, find it among their apps, and take it back.

In this page you add a customer, Maya, to your realm, connect your customer-facing app at `https://app.example.com`,
and follow Maya as she signs in to it. She sees a consent page naming your app and what it asks for, allows it, finds
it on her list of apps, and can take it back whenever she likes.

## Before you begin

- **A realm for your customers.** Here it's `foobar`, at `https://foobar.cvyuh.com`. Keep customers apart from your
  staff: see [Customer realms](../realms/).
- **A realm administrator's session** in `$CVYUH_SESSION`, as [Call the API](../../develop/api/) describes.
- **Your app's return address**, here `https://app.example.com/callback`, and an OAuth 2.0 or OpenID Connect library
  in the app. Most read everything else from the realm's discovery document.

## Connect your app and sign a customer in

<Steps>

1. **Add Maya.** As a realm administrator, create her account:

   ```sh
   curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/user?id=maya" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "uid": "maya", "displayName": "Maya Chen", "email": "maya@example.com",
           "passwordHash": "<a first password>" }'
   ```

   The realm answers `201`. Customers can't sign themselves up yet; see [Registration](../registration/).

2. **Register your app.** Give it the name customers will see on the consent page in `clientName`:

   ```sh
   curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=example-app" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{
       "status": "active",
       "clientName": "Example App",
       "tokenEndpointAuthMethod": "client_secret_basic",
       "clientSecret": "<a long random secret>",
       "grantTypes": ["authorization_code", "refresh_token"],
       "redirectUris": ["https://app.example.com/callback"],
       "scopes": ["openid", "profile", "email"],
       "defaultScopes": ["openid"],
       "allowedResources": ["https://app.example.com/api"],
       "defaultAudience": "https://app.example.com/api"
     }'
   ```

   The realm answers `201`. The secret is stored hashed and never shown again, so keep a copy for the app. The app's
   issuer is `https://foobar.cvyuh.com/oauth2`.

3. **Send Maya to sign in.** When she chooses **Sign in** in your app, the app sends her browser to the realm, with a
   PKCE challenge:

   ```text
   https://foobar.cvyuh.com/oauth2/authorize?response_type=code
     &client_id=example-app
     &redirect_uri=https://app.example.com/callback
     &scope=openid%20profile%20email
     &state=<state>
     &code_challenge=<S256 challenge>&code_challenge_method=S256
   ```

   Maya signs in on the realm's pages with her password. A new realm requires a second step, so the first time she
   also sets up an authenticator app. Once she adds a passkey, she signs in with it alone; see
   [Passkey sign-in for customers](../sign-in/passkeys-for-customers/).

4. **Maya allows your app.** The first time, the realm shows her a consent page: **Example App**, the details it asks
   for, and the account she's signed in as. She chooses **Allow**, and her browser returns to
   `https://app.example.com/callback` with a one-time `code`.

5. **Your app gets its tokens.** Its server exchanges the code at the token endpoint:

   ```sh
   curl https://foobar.cvyuh.com/oauth2/token \
     -u "example-app:<the app's secret>" \
     -d grant_type=authorization_code \
     -d code=<code> \
     -d redirect_uri=https://app.example.com/callback \
     -d code_verifier=<PKCE verifier>
   ```

   The answer carries an `access_token` and an `id_token` naming Maya.

</Steps>

## Check it worked

Maya finds your app on her **Authorized Apps** page in the dashboard. With her own session, the same list is one
call:

```sh
curl https://foobar.cvyuh.com/api/v0/am/me/v1/oauth2/apps \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
```

```json
[
  { "clientId": "example-app", "clientName": "Example App", "scopes": ["openid", "profile", "email"],
    "firstGranted": "<RFC 3339 time>", "lastGranted": "<RFC 3339 time>", "offline": false }
]
```

The next time she signs in to your app, she isn't asked again: the realm remembers what she allowed.

<Aside type="tip" title="Take it back">
  Maya chooses **Revoke** on the app, or sends `DELETE` to
  `https://foobar.cvyuh.com/api/v0/am/me/v1/oauth2/apps/example-app`. The realm answers `204`, every token the app
  held for her stops working at once, and the next time the app asks, she sees the consent page again.
</Aside>

## Next steps

<CardGrid>
  <LinkCard title="The consent page" description="When the realm asks, what it shows, and every setting." href="../consent/consent-page/" />
  <LinkCard title="Passkey sign-in for customers" description="Sign customers in with nothing to type and nothing to phish." href="../sign-in/passkeys-for-customers/" />
  <LinkCard title="A realm per business customer" description="Give each business customer a realm their own administrators run." href="../realms/business-customer-realms/" />
</CardGrid>
