Skip to content

AuthState

ctx.auth: what the adapter knows about the request. It cannot write cookies: only a procedure marked writesCookies gets ctx.auth as an AuthWriter, with setSession, signOut and clearSession.

U extends AuthUser = AuthUser

A = unknown

readonly deviceToken: string | null

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


readonly meta: RequestMeta

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


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.

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


user(): Promise<U | null>

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

Promise<U | null>