Skip to Content
Reference

TanStack Router

Route guards and router wiring for TanStack Router.

import { … } from "@monarcode/session-kit/tanstack-router";

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

redirectIfSignedIn

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

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.

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.

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.

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.

Types

AuthRouter

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

RedirectIfSignedInOptions

Options for redirectIfSignedIn.

Prop

Type

RequireSessionOptions

Options for requireSession.

Prop

Type

RouterAuth

The auth client type in your registered Router's context, as auth, which useAuthClient returns. It's never until the Router is registered.

SessionOutletProps

Props for SessionOutlet.

Prop

Type

On this page