# React Router (https://session-kit.pages.dev/docs/reference/react-router)

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

## Functions

### redirectIfSignedIn

Sends a signed-in user from your sign-in page straight to `redirectTo`, made safe with `safeReturnTo`, inside a loader of your own. When the loader does nothing else, use [`redirectIfSignedInLoader`](#redirectifsignedinloader) instead.

If the session can't be read, such as after a failed refresh or while locked, the sign-in page shows, so the user can sign in again. A `loginPath` that doesn't start with `/` rejects with `INVALID_OPTIONS`.

### redirectIfSignedInLoader

Makes the loader for your sign-in page, for `loader` or `clientLoader`. It reads `?redirectTo=` from the URL and works as [`redirectIfSignedIn`](#redirectifsignedin) does. A `loginPath` that doesn't start with `/` throws `INVALID_OPTIONS` when the loader is made.

### requireSession

Checks for a signed-in user inside a loader of your own, in data or framework mode, and resolves `{ session }`, which holds no tokens. When the loader does nothing else, use [`requireSessionLoader`](#requiresessionloader) as the loader instead.

Call it in a page's loader that calls your API, even under a guarded layout. React Router runs nested routes' loaders at the same time, so the page's doesn't wait for the layout's guard.

Signed-out users go to `loginPath`, with `?redirectTo=` set to the path and query they asked for. React Router leaves the `#fragment` out of a loader's request, so it isn't kept. A locked session counts as signed out, as locking is for native apps and the router adapters don't unlock.

Other errors, such as `REFRESH_FAILED` after a failed refresh, are rethrown to the route's error element, which can offer `auth.retry()`. A `loginPath` or `basename` that doesn't start with `/` rejects with `INVALID_OPTIONS`.

Use `SessionOutlet` as the route's element, and call `useAuthRevalidation`, so the loader runs again when the session changes.

### requireSessionLoader

Makes the loader that guards a route, for `loader` or `clientLoader`, so only signed-in users get through. It resolves `{ session }`, typed with the client's user, and works as [`requireSession`](#requiresession) does.

Pass `loginPath` or `basename` when they aren't the defaults. A `loginPath` or `basename` that doesn't start with `/` throws `INVALID_OPTIONS` when the loader is made.

### useAuthRevalidation

Runs your loaders again when the session changes, so guards such as `requireSession` check it again. That's on sign-in, sign-out, a switch of account, a profile update, a lock, or a refresh that fails once the access token has expired. A token refresh alone doesn't, nor does a failed refresh while the token is still valid.

Call it once in the root layout, in data or framework mode. Declarative mode has no loaders, and React Router throws when the hook runs there.

It watches `client`, or the nearest `AuthProvider`'s, and doesn't mount the client itself. Throws `INVALID_OPTIONS` without either.

## Components

### SessionOutlet

Renders child routes only while someone is signed in, in any mode. It needs an `AuthProvider` above it, or a `client`.

Signed-out users, and a locked session, go to `loginPath`, with `?redirectTo=` set to where they were. In declarative mode, that makes it the guard.

In data or framework mode, render it from a route whose loader returns `requireSession`'s result. When the account changes, it shows `pending` until the loader has run again, so no child sees the last account's data.

Child routes remount for each account, but not on token refreshes or profile updates. UI the route renders around the outlet, such as a sidebar, isn't guarded this way. Read the user there from state, where it may be `null`.

Throws `INVALID_OPTIONS` when it has neither an `AuthProvider` around it nor a `client`.

## Types

### RedirectIfSignedInLoader

The loader that [`redirectIfSignedInLoader`](#redirectifsignedinloader) makes. It reads `redirectTo` from the request's URL, and resolves `null` for a signed-out user.

### RedirectIfSignedInLoaderOptions

Options for [`redirectIfSignedInLoader`](#redirectifsignedinloader).

### RedirectIfSignedInLoaderOptions

Options for redirectIfSignedInLoader.

| Prop         | Type    | Description                                                                                              |
| ------------ | ------- | -------------------------------------------------------------------------------------------------------- |
| `loginPath?` | `union` | Your sign-in page's path, or several. `redirectTo` never sends the user back to one. Default: `"/login"` |

### RedirectIfSignedInOptions

Options for [`redirectIfSignedIn`](#redirectifsignedin).

### RedirectIfSignedInOptions

Options for redirectIfSignedIn.

| Prop         | Type      | Description                                                                                              |
| ------------ | --------- | -------------------------------------------------------------------------------------------------------- |
| `loginPath?` | `union`   | Your sign-in page's path, or several. `redirectTo` never sends the user back to one. Default: `"/login"` |
| `redirectTo` | `unknown` | Where to send a signed-in user, usually the `redirectTo` search param. It's checked with `safeReturnTo`. |

### RequireSessionLoader

The loader that [`requireSessionLoader`](#requiresessionloader) makes. It resolves `{ session }`, typed with the client's user.

### RequireSessionLoaderOptions

Options for [`requireSessionLoader`](#requiresessionloader).

### RequireSessionLoaderOptions

Options for requireSessionLoader.

| Prop         | Type     | Description                                                                                                                              |
| ------------ | -------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| `loginPath?` | `string` | Your sign-in page's path, starting with `/`. Use the same one as `SessionOutlet`'s. Default: `"/login"`                                  |
| `basename?`  | `string` | The router's `basename`, if it has one. Loaders see URLs that include it, but redirects add it again, so `redirectTo` must leave it out. |

### RequireSessionOptions

Options for [`requireSession`](#requiresession).

### RequireSessionOptions

Options for requireSession.

| Prop         | Type     | Description                                                                                                                              |
| ------------ | -------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| `loginPath?` | `string` | Your sign-in page's path, starting with `/`. Use the same one as `SessionOutlet`'s. Default: `"/login"`                                  |
| `basename?`  | `string` | The router's `basename`, if it has one. Loaders see URLs that include it, but redirects add it again, so `redirectTo` must leave it out. |
| `request`    | `object` | The loader's `request`. Signed-out users come back to its URL after signing in.                                                          |

### SessionOutletProps

Props for [`SessionOutlet`](#sessionoutlet).

### SessionOutletProps

Props for SessionOutlet.

| Prop           | Type              | Description                                                                                                                                                                 |
| -------------- | ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `loginPath?`   | `string`          | Your sign-in page's path, starting with `/`. Use the same one as `requireSession`'s. Default: `"/login"`                                                                    |
| `pending?`     | `ReactNode`       | Shown while the saved session is read and, in data or framework mode, until the loader has accepted a new session.                                                          |
| `unavailable?` | `function`        | Shown when the session is `unavailable`, such as after a failed refresh (UnavailableProps). Without it, the error is thrown to the nearest error boundary or error element. |
| `client?`      | `RetryableSource` | The client to watch. Default: the nearest `AuthProvider`'s.                                                                                                                 |

### 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`. |