# Choose online or offline access

> Which app access ends when the user signs out or their session times out, and which keeps working while they're away.

Every grant to an app is either **online**, tied to the user's session, or **offline**, kept while the user is away.
Online access is the safer default: closing the session closes everything the user gave apps along the way. Offline
access is for apps that genuinely work while the user is away, such as a nightly sync, and the user always sees and
agrees to it.

| | **Online** (the default) | **Offline** |
|---|---|---|
| Asked for with | any scopes without `offline_access` | `offline_access` |
| Ends when they sign out, or the session times out | Yes, everywhere at once | No |
| Access tokens last | no longer than the session | their normal lifetime |
| Consent | as usual | always asked, every time it is requested |
| Ends when | the session ends, or the refresh tokens run out | the refresh tokens run out, or the user removes the app |

**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 client ID of each app that should get offline access.

## Set it up

<Steps>

1. Offline access is offered by default: the realm's `scopes` include `offline_access`. To offer only online access,
   read the realm's list with `GET https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings`, and send it back without
   `offline_access`:

   ```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 '{ "scopes": ["…existing scopes, without offline_access"] }'
   ```

   The list you send replaces the realm's whole list, so keep every other scope in it.

2. Let an app ask for offline access: read the client with `GET https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/<client-id>`, and send
   its `scopes` back with `offline_access` 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 '{ "scopes": ["…existing scopes", "offline_access"] }'
   ```

3. Set how long sessions last, and so how long online access lasts. These are the defaults, two hours at most and 30
   idle minutes:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/sessionSettings/sessionSettings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "maxSessionMinutes": 120, "maxIdleMinutes": 30 }'
   ```

</Steps>

## What users and apps see

- **Online:** nothing special for the user; signing out ends the app's access too. After the session ends, the app's
  refresh is refused and its access token reads inactive, so it sends the user to sign in again.
- **Offline:** the consent page says the app asks for access while the user is away, every time it asks. The app's
  refresh keeps working until the refresh lifetime runs out.

## Check it worked

Sign in to an app with online access, then sign out of the realm. The app's next refresh is refused.

For an app with offline access, the token response carries a `refresh_token`, and the user's `TOKEN_ISSUED` record
shows `offline_access` among the granted scopes. Find it in **Audit → Authentication** in the dashboard.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| No refresh token for an app that asked for `offline_access` | The realm's or the client's `scopes` lack it, the client lacks `refresh_token` in `grantTypes`, or the user didn't allow it | Check steps 1 and 2; the user must click **Allow** |
| The consent page shows every time | Offline access is always asked for, by design | Ask for `offline_access` only when the app needs it |
| An app keeps working after the user signs out | It holds offline access | Revoke its grant, or the user removes it on **Authorized Apps** |

## Undo it

Send the client's `scopes` back without `offline_access`, so it can't ask again. Offline grants already made last
until their refresh tokens run out; end them sooner by revoking the refresh token
([Revocation](../../../applications/revoke-access/revocation/)), or the user removes the app.

## Related

- [Refresh tokens](../../../applications/single-sign-on/refresh-tokens/): lifetimes and families.
- [Logout](../../../applications/single-sign-on/logout/): what ending a session revokes.
- [Consent](../../../../customer-identity/consent/): why offline access is always asked.
- [Let users remove apps they allowed](../../../../customer-identity/guides/people-remove-apps/): how a user ends offline access.
- [Keep apps signed in](../keep-apps-signed-in/): refresh and rotation.
- [Sign users out from an app](../sign-out-from-an-app/): ending the session on purpose.
