# Sign users in to a single-page app

> Register a browser app that holds no secret, so users sign in through the realm and the page calls the realm directly.

An app that runs entirely in the browser, such as a dashboard with no server of its own, can't keep a secret: anything
in the page can be read by whoever opens it. You register it as a **public client**. It gets the same sign-in as a web app, proves it started the sign-in with PKCE alone, and calls the realm's token, revocation
and userinfo endpoints straight from the page.

**Before you start:**

- **Who can do it:** a realm administrator, in a group that holds `RealmOwner`
  ([Delegated administration](/platform/realms/delegated-administration/)). `RealmViewer` can read the settings but
  not change them.
- **Where:** today this is done through the API; [Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`.
- **First:** the realm offers the scopes the app needs (step 1 of [Sign users in to a web app](../sign-in-web-app/)).

## Register the app

<Steps>

1. Register the app as a public client:

   ```sh
   curl 'https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=ops-dashboard' \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{
       "status": "active",
       "clientName": "Ops Dashboard",
       "tokenEndpointAuthMethod": "none",
       "grantTypes": ["authorization_code", "refresh_token"],
       "redirectUris": ["https://ops.example.com/callback"],
       "postLogoutRedirectUris": ["https://ops.example.com/"],
       "scopes": ["openid", "profile", "offline_access", "ops:read"],
       "defaultScopes": ["openid", "ops:read"],
       "allowedResources": ["https://ops.example.com/api"],
       "defaultAudience": "https://ops.example.com/api"
     }'
   ```

   `tokenEndpointAuthMethod: none` makes it public; leave out `clientSecret`. `refreshTokenRotation` needs no setting,
   since a public client always rotates.

2. If the page runs on a different site from its return address, list that site in `webOrigins`, keeping `+` for the
   return addresses' own sites:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/ops-dashboard \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "webOrigins": ["+", "https://ops-app.example.com"] }'
   ```

   A site is written exactly as a browser sends it: scheme and host, a port only if it isn't the default, no path.
   When the page runs on the same site as its return address, skip this step.

3. Give the app its settings: the client ID `ops-dashboard` and the issuer `https://foobar.cvyuh.com/oauth2`. There's no
   secret.

</Steps>

## What the app sees

- The page redeems the code at `https://foobar.cvyuh.com/oauth2/token` with its PKCE verifier and no secret.
- The realm lets the page read the answer because the app's site is one of its `webOrigins`. By default that's the
  site of each registered return address (`webOrigins: ["+"]`). A page on any other site can't read the answer. See
  [Browser apps](../../../applications/single-sign-on/browser-apps/).
- Each refresh token works **once**. Every refresh returns a new one, and if an old one is ever used again, the realm
  ends the whole family. That is how a stolen refresh token is caught.
- A public app can never use [client credentials](../../../applications/tokens/client-credentials/).

Every sign-in must carry a PKCE challenge using the `S256` method. A request without one, or with `plain`, is refused.

Users see the same as for a web app: the realm's sign-in page, consent the first time, then back in the app.

## Check it worked

Sign in from the page as a test user, and watch the token call in the browser's developer tools: it should succeed
with no CORS error. A CORS error means the page's site isn't in `webOrigins`.

Each sign-in is on the user's trail as a `LOGIN` record and a `TOKEN_ISSUED` record for the client; a refresh token
used twice is a `TOKEN_ISSUED` record with result `FAILED` and the reason "the refresh token was already used". Find
them in **Audit → Authentication** in the dashboard.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A CORS error on the token call | The page's site isn't in `webOrigins` | Add it exactly as the browser sends it (step 2) |
| `invalid_client` at the token endpoint | The page sent a secret, or the client isn't registered with `none` | Send no secret; register `tokenEndpointAuthMethod: none` |
| `invalid_grant`, "already used", and the user is signed out | Two tabs refreshed with the same token, or a copy was used | Refresh from one place in the page; if neither, treat it as theft |
| The user is sent to sign in once a day | The refresh family's lifetime ran out | Expected; see [Keep apps signed in](../../sessions/keep-apps-signed-in/) |

## Undo it

`PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/ops-dashboard` with `{ "status": "inactive" }` turns the app off.

<Aside type="tip" title="A stronger guarantee">
  Bind the app's tokens to a key the page holds, so a copied token is useless:
  [Bind tokens to a key](../../services/bind-tokens-to-a-key/).
</Aside>

## Related

- [Browser apps](../../../applications/single-sign-on/browser-apps/): `webOrigins` and what the realm answers from the page.
- [Authorization code with PKCE](../../../applications/single-sign-on/authorization-code/): the flow the app uses.
- [Refresh tokens](../../../applications/single-sign-on/refresh-tokens/): rotation and families.
- [Sign users in to your own native app](../native-app-sign-in/): your own app's sign-in screens.
