# Expo (https://session-kit.pages.dev/docs/reference/expo)

Import these from `@monarcode/session-kit/expo`. To set it up, see
[Expo Router](https://session-kit.pages.dev/docs/setups/expo).

## Functions

### deepLinkHandler

Makes `redirectSystemPath` for `app/+native-intent.tsx`. It keeps each incoming link with [`rememberDeepLink`](#rememberdeeplink), so `SessionGate` opens it after sign-in, and returns the path unchanged.

Use `rememberDeepLink` itself when your `redirectSystemPath` does more, such as rewriting old links.

Throws `INVALID_OPTIONS` for a `loginPath` that doesn't start with `/`. It throws when it's made, so a link never meets the error.

See [`RememberDeepLinkOptions`](#rememberdeeplinkoptions) to set your sign-in screen's path.

### rememberDeepLink

Keeps an incoming deep link, so `SessionGate` can open it after the user signs in. Without it, `Stack.Protected` sends a signed-out user from a guarded screen to one they may see, and the link is lost.

Call it from `redirectSystemPath` in `app/+native-intent.tsx`, which Expo Router calls with every URL that opens the app. `url` can be a full URL or a path. Return the path unchanged, so a signed-in user still lands on it.

The path is checked with `safeReturnTo`, so the sign-in path, the root, and anything that could leave the app are ignored. A newer link replaces an older one. The link is kept in memory only, so an app restart forgets it. What `SessionGate` does with it:

- **Signed in already**: drops it, since Expo Router opens it as usual.
- **Signed out or locked**: opens it with `router.replace` after the next sign-in or unlock, once `SessionGate`'s `ready` is `true`.
- **A screen anyone may see**: drops it once that screen is showing.

Throws `INVALID_OPTIONS` for a `loginPath` that doesn't start with `/`. Expo Router warns that a throw in `redirectSystemPath` can crash the app. [`deepLinkHandler`](#deeplinkhandler) throws when it's made instead.

### secureStorage

Saves the session with `expo-secure-store`: in the iOS keychain, and encrypted with an Android Keystore key on Android. Pass it as `createAuth`'s `storage` in Expo apps.

- Values over 2048 bytes, which some iOS releases refuse to store, are split across keys. A write cut short, for example when the app is killed, keeps the previous value whole. A value over 128 KB fails to save, with `PERSISTENCE_FAILED`.
- Reads are synchronous, so with a synchronous user schema the session is restored before the first render. Writes and removals are asynchronous.
- On iOS, a session left in the keychain by an earlier install is removed unless `clearOnReinstall` is `false`. A marker file in the app's documents directory, which deleting the app removes, tells installs apart.
- Data that can't be decrypted, as after Android restores a backup onto another device, reads as signed out and is removed. Other read failures, such as a locked iOS keychain, make the session `unavailable`.

Unlike `webStorage`, it has no `subscribe` or `lock`, because an app has no other tabs to follow or to share refreshes with.

See [`SecureStorageOptions`](#securestorageoptions) for iOS keychain access and reinstalls.

## Components

### SessionGate

Wraps the root navigator in `app/_layout.tsx`. It adds what Expo Router's `Stack.Protected` lacks: a splash screen held while the session is restored, a screen for when access is unavailable, and deep links kept for after sign-in.

Render it inside `AuthProvider`, or pass a `client`, which it mounts. Without either, it throws `INVALID_OPTIONS`.

- **While the session is restored**, and until `ready`, it renders nothing, so the splash screen stays up, and hides the splash screen once both are done. The navigator mounts only then, so a cold-start deep link isn't sent to the sign-in screen first. Call `SplashScreen.preventAutoHideAsync()` at the top of the root layout.
- **While access is unavailable** it renders `unavailable({ error, retry })` instead of the navigator. Without `unavailable`, it hides the splash screen and throws the error to the root layout's `ErrorBoundary`.
- **While locked** it renders the navigator, so its guards can show an unlock screen. Neither unlocking nor a password sign-in from the lock screen remounts the navigator.
- **When the app returns to the foreground** it re-checks the session, because timers don't run while iOS suspends the app. An access token that expired meanwhile is refreshed. A refresh that already failed stays failed until `retry()`.
- **After a sign-in or unlock** it opens the deep link `rememberDeepLink` kept, with `router.replace`, once `ready`.
- **When one account replaces another** without a sign-out between them, it remounts the navigator, so no screen keeps the previous account's component state. The screens that were open stay open. A sign-out already removes the guarded screens.

## Types

### DeepLinkHandler

The `redirectSystemPath` that [`deepLinkHandler`](#deeplinkhandler) makes, for `app/+native-intent.tsx`.

### RememberDeepLinkOptions

Options for [`rememberDeepLink`](#rememberdeeplink) and [`deepLinkHandler`](#deeplinkhandler), the same as `safeReturnTo`'s. Set `loginPath` when your sign-in screen isn't `/login`, so links to it are dropped rather than kept.

### RememberDeepLinkOptions

Options for rememberDeepLink and deepLinkHandler, the same
as `safeReturnTo`'s. Set `loginPath` when your sign-in screen isn't
`/login`, so links to it are dropped rather than kept.

| Prop         | Type    | Description                                                                                                                                                                                                                          |
| ------------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `loginPath?` | `union` | Your sign-in page's path, or several. A path to it is replaced with `"/"`, so signing in never sends the user back to sign in. Matched by path, ignoring case, percent-encoding, trailing slashes and any query. Default: `"/login"` |

### SecureStorageOptions

Options for [`secureStorage`](#securestorage). Both are for iOS: when the keychain may be read, and what happens to a session an earlier install left there.

### SecureStorageOptions

Options for secureStorage. Both are for iOS: when the keychain may
be read, and what happens to a session an earlier install left there.

| Prop                  | Type     | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| --------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `keychainAccessible?` | `number` | When iOS lets the app read the session: one of SecureStore's constants, such as `AFTER_FIRST_UNLOCK`. An app that refreshes in a background task while the device is locked needs `AFTER_FIRST_UNLOCK`. Passed to SecureStore unchanged. If the keychain can't be read when the app starts, the session is `unavailable`, not signed out. A refresh that runs while it's locked can't save the new tokens, and that signs the user out. Default: `WHEN_UNLOCKED` |
| `clearOnReinstall?`   | `union`  | Whether a reinstalled app starts signed out. iOS keeps keychain items after the app is deleted, so a session an earlier install left is removed before it's read. `false` restores it instead. Default: `true`                                                                                                                                                                                                                                                   |

### SessionGateProps

Props for [`SessionGate`](#sessiongate): the root navigator, what to show while access is unavailable, and what else the splash screen waits for.

### SessionGateProps

Props for SessionGate: the root navigator, what to show while access
is unavailable, and what else the splash screen waits for.

| Prop           | Type              | Description                                                                                                                                                                                                                                                                                                                                                                                                           |
| -------------- | ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `children?`    | `ReactNode`       | The root navigator, usually a `Stack` guarded with `Stack.Protected`.                                                                                                                                                                                                                                                                                                                                                 |
| `unavailable?` | `function`        | Shown instead of the navigator while access is unavailable, such as after a failed refresh. Without it, the error is thrown to the root layout's `ErrorBoundary`. That boundary's `retry` only renders again, and a failed refresh stays failed until `auth.retry()`, so call both.                                                                                                                                   |
| `client?`      | `RetryableSource` | The client to watch. Default: the nearest `AuthProvider`'s.                                                                                                                                                                                                                                                                                                                                                           |
| `ready?`       | `union`           | Whether the rest of the app is ready for its first screen, such as fonts or remote config. Until it's `true`, and while the session is restored, the gate renders nothing and keeps the splash screen up. It only holds the splash screen the first time, even if the gate remounts. Setting it `false` later changes nothing on screen, but the deep link opened after a sign-in still waits for it. Default: `true` |

### UnavailableProps

What the `unavailable` render prop of an adapter component, such as React Router's `SessionOutlet` or Expo's `SessionGate`, receives.

### UnavailableProps

What the `unavailable` render prop of an adapter component, such as React
Router's `SessionOutlet` or Expo's `SessionGate`, receives.

| Prop    | Type       | Description                                                                     |
| ------- | ---------- | ------------------------------------------------------------------------------- |
| `error` | `union`    | Why access is unavailable, such as a failed refresh or a locked keychain.       |
| `retry` | `function` | Tries again, as `auth.retry()` does. A repeated failure stays in `state.error`. |