# Sign users out from an app

> Let an app sign the user out of the realm and bring them back to the app, and what happens when the request can't prove whose session it is.

**Sign out** in an app should end the user's session with the realm, not only the app's own. Every online grant ends
with it, in every app, and the user lands back at a page
of the app's choosing. This is for any app that signs users in through the realm.

**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:** have the app's client ID and the address it wants users to land on.

## Set it up

<Steps>

1. Register where the browser may return after sign-out. Read the client first with
   `GET https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/<client-id>`, then send its `postLogoutRedirectUris` back with the address
   added:

   ```sh
   curl -X PATCH 'https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/<client-id>' \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "postLogoutRedirectUris": ["…existing addresses",
                                  "https://wiki.example.com/"] }'
   ```

   The list you send replaces the client's whole list. The return address must match one registered here exactly,
   port included.

2. Have the app's **Sign out** send the browser to:

   ```text
   https://foobar.cvyuh.com/oauth2/logout?id_token_hint=<the ID token>
       &post_logout_redirect_uri=https://wiki.example.com/
       &state=<anything the app wants back>
   ```

   An expired ID token is still a good hint: sessions often outlive their ID tokens.

3. Optionally, change how long the confirm page stays answerable, from 10 to 3600 seconds (600 by default):

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "logoutChallengeLifetimeSecs": 600 }'
   ```

</Steps>

## What the user sees

| The app sends… | The user sees |
|---|---|
| their ID token as a hint, and a registered return address | Signed out at once, back at the app's page |
| a hint that names another session, or no hint | A page asking them to confirm, then signed out |
| a registered return address with only its `client_id` | The confirm page, then back at the app's page |
| a hint the realm didn't sign | A signed-out page with an error; never sent back to the app |
| anything but a hint the realm didn't sign, when they're already signed out | Back at the app's page if it sent a registered return address, else the signed-out page; no error |

<Aside type="note" title="Why the confirm page">
  It stops a link on another site from signing users out without their knowing. Only the session being ended can
  answer it.
</Aside>

## Check it worked

Sign in to the app, click **Sign out**, and you land on the registered page. Opening another app that shared the
session now asks you to sign in. The realm's own sign-out, in the dashboard, has the same effect.

The sign-out is on the user's trail as a `LOGOUT` record naming the session. Find it in **Audit → Authentication** in
the dashboard.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| The user lands on the realm's signed-out page, not the app's | The return address isn't registered exactly, port included, or the app sent neither a hint nor its `client_id` | Register the address as the app sends it, and send the hint |
| The user sees a confirm page every time | The app sends no hint, or a hint from another session | Send the ID token from the current sign-in as `id_token_hint` |
| An error page, and the user isn't sent back | The hint wasn't signed by the realm for this app | Send the app's own ID token, unchanged |
| Confirming does nothing, or is refused | The user waited past `logoutChallengeLifetimeSecs` | Start the sign-out again, or raise the setting |
| Another app still shows the user signed in | That app keeps its own session | [Tell other apps about a sign-out](../tell-apps-about-sign-out/) |

## Undo it

Send the client's `postLogoutRedirectUris` back without the address. Sign-outs from the app then end on the realm's
signed-out page.

## Related

- [Logout](../../../applications/single-sign-on/logout/): every sign-out path and what it revokes.
- [Tell other apps about a sign-out](../tell-apps-about-sign-out/): so apps end their own sessions too.
- [Choose online or offline access](../online-and-offline-access/): which access survives a sign-out.
