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

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

## Functions

### connectAuth

Starts auth for a Router whose context holds the client as `auth`. It restores the session, runs expiry timers, follows other tabs, and runs guards such as `requireSession` again when the session changes. Call it once, beside the Router.

When the session changes, it also clears the Router's cached and preloaded route data, so one account's data never shows for the next.

It returns a function that stops. Calling it again for the same Router returns that same function.

Throws `INVALID_OPTIONS` when the Router's context has no client as `auth`.

On a server, such as when TanStack Start renders its SPA shell, it does nothing. The shell keeps the `initializing` state, which shows your pending UI.

### redirectIfSignedIn

Guards your sign-in page in `beforeLoad`, so a signed-in user goes straight to `redirectTo`, made safe with `safeReturnTo`.

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

### requireSession

Guards a route in `beforeLoad`, so only signed-in users get through. They get `{ session }` in route context, typed and never `null`, with no tokens in it.

Signed-out users go to `loginPath`, with `?redirectTo=` set to where they were going. 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 component, which can offer `auth.retry()`. A `loginPath` that doesn't start with `/` rejects with `INVALID_OPTIONS`.

Render the route's children with `SessionOutlet`, and call `connectAuth`, so the guard runs again when the session changes.

### useAuth

Reads the current auth state of the client in Router context, or the part `selector` picks, and re-renders when it changes. With a selector, it re-renders only when that part changes.

Typed by your registered Router, as [`useAuthClient`](#useauthclient) is. Throws `INVALID_OPTIONS` when Router context has no `auth`.

### useAuthClient

Returns the auth client from Router context, where it's `auth`, for actions such as `signIn` and `signOut`. It needs no `AuthProvider`: `connectAuth` mounts the client.

The client's type comes from your registered Router ([`RouterAuth`](#routerauth)). Router context is per request in TanStack Start, so this is safe during server rendering.

Throws `INVALID_OPTIONS` when Router context has no `auth`.

## Components

### SessionOutlet

Renders the child routes of a route guarded by `requireSession`, but only while the session it accepted is still the signed-in one.

After a change, such as a sign-out or another account signing in, it shows `pending` until the guard has run again. A locked or `unavailable` session shows `pending` too, until the guard redirects or throws.

Child routes remount for each account, so nothing from one stays mounted for the next. Token refreshes and profile updates keep them mounted.

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 neither an `AuthProvider` nor Router context has a client.

## Types

### AuthRouter

What [`connectAuth`](#connectauth) needs from a TanStack Router: the client in its context as `auth`, and the methods that run guards again and clear cached route data. A Router made by `createRouter` with `context: { auth }` fits.

Use it to type a function that takes your Router.

### RedirectIfSignedInOptions

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

### RedirectIfSignedInOptions

Options for redirectIfSignedIn.

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

### RequireSessionOptions

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

### RequireSessionOptions

Options for requireSession.

| Prop         | Type     | Description                                                                                              |
| ------------ | -------- | -------------------------------------------------------------------------------------------------------- |
| `location`   | `object` | The route's location, from `beforeLoad`'s `location`. Signed-out users come back to it after signing in. |
| `loginPath?` | `string` | Your sign-in page's path, starting with `/`. Default: `"/login"`                                         |

### RouterAuth

The auth client type in your registered Router's context, as `auth`, which [`useAuthClient`](#useauthclient) returns. It's `never` until the Router is registered.

### SessionOutletProps

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

### SessionOutletProps

Props for SessionOutlet.

| Prop       | Type         | Description                                                                                |
| ---------- | ------------ | ------------------------------------------------------------------------------------------ |
| `pending?` | `ReactNode`  | Shown while the guard checks the session again, such as after a sign-out.                  |
| `client?`  | `AuthSource` | The client to watch. Default: the nearest `AuthProvider`'s, then `auth` in Router context. |