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

```ts
import { … } from "@monarcode/session-kit/react-router";
```

Guide: [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.

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

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

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

## Components

### SessionOutlet

Renders child routes only while someone is signed in, in any mode. It needs an `AuthProvider` above it, or 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`. |