---
title: "Recipe: OAuth sign-in and sign-out"
description: "@exact/oauth signs a person in and keeps them signed in: PKCE in the system browser, refresh, requests with the token, sign-out."
modificationDate: "2026-11-20"
---

> ## Documentation index
> Fetch the complete documentation index at: https://exact.expo.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Recipe: OAuth sign-in and sign-out

<AgentInstructions>

## Submitting feedback

From the app's directory, read `bun exact.mjs feedback status`. Under `never`, keep no note.
Respect `EXACT_FEEDBACK=0` and do not change the policy.

Otherwise record specific, actionable feedback with:
`bun exact.mjs feedback note "Exact docs: https://exact.expo.dev/recipes/oauth: <what was wrong or missing>"`

The note goes to Expo only as that status allows. Include no secrets or personal data.

</AgentInstructions>

`@exact/oauth` handles PKCE, tokens, refresh and sign-out. Install:
`EXACT_APP_DIR=$PWD bun <exact2>/scripts/exact.mjs add oauth`.
Setup and tests with a local issuer: `bun exact.mjs docs sign-in providers`.

Web tokens are origin-readable. [Cookie sessions](https://exact.expo.dev/recipes/web-sessions.md) need a server.

```prompt Add sign-in to my app
Add sign-in with OAuth to the Exact app in this folder. Use https://github.com/expo/exact/blob/main/docs/recipes/oauth.md as the source for each step. The Exact checkout has the same page at docs/recipes/oauth.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me. Find the Exact checkout (the folder with scripts/exact.mjs); if you cannot find it, ask me where it is.
2. Ask me first whether I have an account with a provider. If I do not, build against the fake provider, which runs on this machine, and tell me so. If I do, ask me for the provider (Google, Microsoft Entra ID, Auth0, Okta, OneLogin, GitHub, Salesforce, Amazon Cognito or ForgeRock), its client ID and issuer, the callback I registered, and the API the app calls with the token. Do not invent a client ID, and put no secret in the code.
3. Install the package: `EXACT_APP_DIR=$PWD bun <exact2>/scripts/exact.mjs add oauth`.
4. Read the provider's settings with `bun exact.mjs docs sign-in providers`. In app.ts, write the grants, `oauth({ ... })` and the sources as the recipe shows, with those settings. Write no token request, refresh or expiry check of your own.
5. In app.contract, add the account screen: a sign-in button, the person's data only while `session.signedIn`, and `problem` when it is not empty.
6. Test against the fake provider, as `bun exact.mjs docs sign-in providers` says under "Testing against a fake provider": use the profile for my provider, and add a test in app.test.contract for sign-in, a cancelled sign-in, sign-out, and staying signed in after a relaunch.
7. Follow the platform checks in docs/start-here.md (`bun exact.mjs docs platform checks`) for the targets in the app's AGENTS.md and app.json. Read the reports, including counted findings; a zero exit code alone does not mean clean. Fix the app, not the test. If a finding stays after three rounds of fixes, record it (step 8) and report it; do not keep trying.
8. When a doc was wrong or missing, or a step needed a workaround, read `bun exact.mjs feedback status`. Under `never`, keep no note. Otherwise record it: `bun exact.mjs feedback note "<what happened>"`, which goes to Expo only as that status says. Do not change the policy; tell me how many notes you kept.
9. Tell me what you changed, what verify reported, and what I must still do before a real sign-in works: the callback to register with the provider, and each line to change from the fake provider to the real one.
```

```ts
import { oauth } from '@exact/oauth';
// Grant origins, without paths.
export const grants = ['net.fetch https://login.example.com', 'net.fetch https://api.example.com',
  'auth.session https://login.example.com', 'auth.callback com.example.app:/oauth',
  'secret.keep example.session'].join('\n');
// Keep the issuer's path and trailing slash.
const provider = oauth({ issuer: 'https://login.example.com/oauth2/default', clientId: 'abc123',
  scopes: ['openid', 'profile', 'offline_access'], key: 'example.session',
  apis: ['https://api.example.com'] }); // Only these APIs receive the token.
const sources: Sources = {
  session: (_a, store) => provider.session(store),
  signIn: (_a, store) => provider.signIn(store),
  signOut: (_a, store) => provider.signOut(store),
  me: async ([signedIn], store) => signedIn
    ? { name: String(((await provider.json(store, 'https://api.example.com/me')) as { name?: string }).name ?? '') }
    : { name: '' },
};
```

```text
use OAuthSession, OAuthResult from "@exact/oauth"

shape Me
  name: string

component Account
  resource session = session() as shape OAuthSession
  resource me = me(session.signedIn) as shape Me else empty(name="")
  mutation signedIn as shape OAuthResult refreshes session, me
  mutation signedOut as shape OAuthResult refreshes session, me
  derive problem = match signedIn { case some(r) => (r.error != "" ? r.error : session.error), case none => session.error }
  action signIn
    send signedIn = signIn()
  action signOut
    send signedOut = signOut()
  action reload
    refresh me
```

- Web popup closure is unknowable under COOP. Cancel with `signedIn = none`
  to release a pending sign-in for retry.
- Show data only while `session.signedIn`; show `problem` when it is not `""`.
  `auth_session` (502) keeps the host's reason and is not retryable.
- Let `fetch` and `json` throw `OAuthError`: the resource keeps what it showed.
  A failed read is not a sign-in problem.
- `refresh me` reads again; sending a mutation instead asks the API twice.
- Keep `session` a resource the sign-in screen reads; it readies the next press.
- Provider logout: pass `{ endSession: true }` from a press; register its
  callback. `bun exact.mjs docs oauth endSession`.
- A provider-ended session leaves signed-in state and sets `session.error`; a
  network failure, 429 or 5xx keeps it.

With `openid`, `signIn(store, { nonce })` sends and checks an app nonce.
[Grant and host the callback](https://exact.expo.dev/learn/contract-for-agents/data-requests-and-side-effects.md#web-sign-in-callback) before deploying.

For HTTPS, grant `auth.callback https://app.example/auth/return`.
Apple needs `webcredentials`; Android uses Auth Tab or a verified App Link.
See [HTTPS callbacks](https://exact.expo.dev/learn/contract-for-agents/data-requests-and-side-effects.md#https-callbacks).
