function verifyCsrfToken
Since 0.2.0
verifyCsrfToken(
ctx: IRequestContext,
options?: CsrfFormOptions
): Promise<void>

Verifies the request's CSRF token against the session's, throwing on any mismatch.

Exported so a handler or framework action can validate inline — React Router actions conventionally do their own validation rather than relying on middleware. The middleware calls exactly this function, so the two can never disagree.

Reading the body here is safe: every body read is memoized (M87), so the parse is cached and the handler can still read the body afterwards — including a multipart body, which is read now too (M94b) through the same shared accessor the upload middleware uses. The configured header is read first, so a client that sends the token in the header triggers no body read at all.

Examples

Example 1

export async function action({ context }: ActionFunctionArgs) {
  await verifyCsrfToken(context.get(ctxKey));
  // … safe to mutate
}

Parameters

ctx: IRequestContext

The request context

optional
options: CsrfFormOptions

CSRF options; defaults match the plugin's

Return Type

Promise<void>

Throws

CsrfTokenMismatchError

If the token is absent, malformed, or wrong

SessionMiddlewareMissingError

If the session middleware did not run

Usage

import { verifyCsrfToken } from "session-plugin/src/index.ts";