Skip to Content
Reference

React Router server

Server sessions in React Router's framework mode.

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

Guide: React Router with server rendering.

Functions

cookieJarMiddleware

Server middleware that gives each request a CookieJar, found with routerJar. It sets the cookies written to the jar on the response, redirects and error responses included.

redirectIfSignedIn

Makes the loader for your sign-in page, which sends a signed-in user on to the page's redirectTo search param, made safe with safeReturnTo. Signed-out users see the page. React Router adds the app's basename to the redirect.

requireSession

Makes server middleware that lets only signed-in users through to a route and the routes nested in it (SessionGuard). Their loaders and actions get the session from its session(context), never null.

routerAuth

Returns a function that gives the session of the request React Router is handling (RequestAuth), with every serverAuth method and no cookie jar. Call it with context in server middleware, loaders and actions, after cookieJarMiddleware. Make it once, at module level.

routerJar

The CookieJar for the request React Router is handling, to pass to serverAuth from @monarcode/session-kit/server. routerAuth passes it for you. Call it with context in server middleware, loaders and actions, after cookieJarMiddleware. Every call during one request returns the same jar.

sessionMiddleware

The root route's server middleware, which gives each request a cookie jar and refreshes the session while the response can still set the cookie. Export it as the root route's middleware, or spread it first into a list of your own.

sessionRoute

Makes the loader and action of your session route, a resource route that answers createMirrorAuth's endpoint: the browser client reads the session, signs out and gets the access token through it. It needs sessionMiddleware in the root route.

Types

RouterArgs

The arguments React Router passes to server middleware and loaders, as far as the middleware and guards here read them. It's typed by its shape, like RouterContextLike.

RouterContextLike

The context React Router passes to server middleware, loaders and actions in framework mode: a RouterContextProvider, one per request. routerAuth and cookieJarMiddleware find the request by it. It's typed by its shape, so this entry point doesn't depend on React Router's types.

RouterLoader

A loader typed by its shape, such as redirectIfSignedIn's.

RouterMiddleware

Server middleware for a route's middleware, typed by its shape.

RouterSessionRoute

The loader and action sessionRoute makes, for a resource route.

SessionGuard

Server middleware that lets only signed-in users through, made by requireSession. Call session(context) in the loaders and actions it guards for the session, which is never null there.

SessionGuardOptions

Options for requireSession.

Prop

Type

On this page