UNPKG

remix-utils

Version:

This package contains simple utility functions to use with [React Router](https://reactrouter.com/).

81 lines 3.32 kB
import { unstable_createContext } from "react-router"; /** * Creates a session middleware that uses the provided session storage object * to manage session data. * * The session middleware will read the session data from the request cookies, * and store it in the context for the rest of the middleware or loaders and * actions to use. * * After the middleware chain is done, the session data will be compared to the * initial data, and if it changed, the session will be commited to the storage * and a Set-Cookie header will be added to the response. * * This middleware also helps implement rolling sessions by setting the session * cookie on every response. * * @param sessionStorage The session storage object used by the middleware * @param shouldCommit A function that compares the previous and next session data * to determine if it changed and needs to be commited. Defaults to a function that always returns true. * @returns The session middleware and a function to get the session from the context * * @example * // app/middleware/session.ts * import { sessionStorage } from "~/session"; * import { unstable_createSessionMiddleware } from "remix-utils"; * * export const [sessionMiddleware, getSession] = * unstable_createSessionMiddleware(sessionStorage); * * // app/root.tsx * import { sessionMiddleware } from "~/middleware/session"; * export const unstable_middleware = [sessionMiddleware]; * * // app/routes/_index.tsx * import { getSession } from "~/middleware/session"; * * export async function loader({ context }: Route.LoaderArgs) { * let session = getSession(context); * session.set("key", "value"); // This will be commited automatically * return { data: session.get("data") }; * } * * @example * // app/middleware/session.ts * import { createCookieSessionStorage } from "react-router"; * import { unstable_createSessionMiddleware } from "remix-utils/middleware/session"; * * export const [sessionMiddleware, getSession] = * unstable_createSessionMiddleware((request, context) => { * let url = new URL(request.url); * let domain = url.hostname === "localhost" ? undefined : url.hostname; * return createCookieSessionStorage({ * cookie: createCookie("session", { domain }) * }) * }); */ export function unstable_createSessionMiddleware(sessionStorage, shouldCommit = defaultShouldCommit) { let sessionContext = unstable_createContext(); return [ async function middleware({ request, context }, next) { let storage = typeof sessionStorage === "function" ? sessionStorage(request, context) : sessionStorage; let session = await storage.getSession(request.headers.get("Cookie")); let initialData = structuredClone(session.data); context.set(sessionContext, session); let response = await next(); if (shouldCommit(initialData, structuredClone(session.data))) { response.headers.append("Set-Cookie", await storage.commitSession(session)); } return response; }, function getSession(context) { return context.get(sessionContext); }, ]; } function defaultShouldCommit() { return true; } //# sourceMappingURL=session.js.map