Skip to content

AuthWriter

ctx.auth in a procedure marked writesCookies (WritesCookiesMiddleware on nestjs-trpc). In any other procedure these three do not exist on the type, and at run time they throw an AuthConfigError before writing anything (signOut before the core signs out).

U extends AuthUser = AuthUser

A = unknown

readonly deviceToken: string | null

The trusted-device token from its cookie, for auth.login({ deviceToken }).

AuthState.deviceToken


readonly meta: RequestMeta

Client IP through the trusted hops, user agent and origin: pass it to the flows.

AuthState.meta


readonly session: ValidatedSession<A> | null

The session the core accepted, or null when there is none or it was refused. Under the two-factor plugin’s enforcement it carries twoFactor (grace or enrollment-only) for this request.

AuthState.session

clearSession(cookie): object

After a flow that ended the session itself (auth.sessions.revokeAll): writes the blank cookie it returned and starts a fresh pre-session.

CookieSpec

object

csrfToken: string


csrfToken(): object

The CSRF token for the request’s session or pre-session, for a client that cannot read the csrf cookie: return it from a public query. It is exactly the token this response sets or keeps as the cookie, minted with the pre-session when missing, as on any query sent with GET. The response is marked Cache-Control: no-store, but tRPC lets the headers of your responseMeta replace or join that one: wrap it in authResponseMeta (or pass authResponseMeta() when you have none), which drops these headers yours returns: Cache-Control, Surrogate-Control, CDN-Cache-Control, every *-CDN-Cache-Control, X-Accel-Expires and Edge-Control. A proxy-specific cache header outside this list is not touched; keep such rules off the token route, and keep the query out of any caching rule. On nestjs-trpc 2.14.0 and later, pass it as trpc: { responseMeta: authResponseMeta(yours) }; before 2.14.0 there is no responseMeta, so keep cache-setting middleware off the base path or serve the token from Nest with SessionCookies.csrfToken. The token stays bound to the session or pre-session and the cookie is still required next to it. Whatever your CORS setup lets a page on another origin read, its unsafe requests are refused on the Origin allowlist and Sec-Fetch-Site, whatever the token, so a read token is useless cross-site. Throws an AuthConfigError on a request sent with an unsafe method (where nothing is minted), on a streamed batch (httpBatchStreamLink) and on a subscription, whose headers go out before any procedure runs.

object

csrfToken: string

AuthState.csrfToken


setSession(issued): object

Writes an issued session’s cookie and its trusted-device cookie, plus a CSRF cookie bound to the new session. Call it on sign-in and on every rotation (re-authentication, a password change, revoking the other sessions, verifying an address). The token is returned for the response.

Pick<IssuedSession<unknown>, "cookie" | "deviceCookie" | "session">

object

csrfToken: string


signOut(): Promise<{ csrfToken: string; }>

Signs the request’s session out through the core (auth.logout), clears its cookie and starts a fresh pre-session with a CSRF token bound to it.

Promise<{ csrfToken: string; }>


user(): Promise<U | null>

The session’s user, loaded from the store once per request; null when signed out or deleted.

Promise<U | null>

AuthState.user