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).
Extends
Section titled “Extends”AuthState<U,A>
Type Parameters
Section titled “Type Parameters”U extends AuthUser = AuthUser
A = unknown
Properties
Section titled “Properties”deviceToken
Section titled “deviceToken”
readonlydeviceToken:string|null
The trusted-device token from its cookie, for auth.login({ deviceToken }).
Inherited from
Section titled “Inherited from”
readonlymeta:RequestMeta
Client IP through the trusted hops, user agent and origin: pass it to the flows.
Inherited from
Section titled “Inherited from”session
Section titled “session”
readonlysession: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.
Inherited from
Section titled “Inherited from”Methods
Section titled “Methods”clearSession()
Section titled “clearSession()”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.
Parameters
Section titled “Parameters”cookie
Section titled “cookie”CookieSpec
Returns
Section titled “Returns”object
csrfToken
Section titled “csrfToken”csrfToken:
string
csrfToken()
Section titled “csrfToken()”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.
Returns
Section titled “Returns”object
csrfToken
Section titled “csrfToken”csrfToken:
string
Inherited from
Section titled “Inherited from”setSession()
Section titled “setSession()”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.
Parameters
Section titled “Parameters”issued
Section titled “issued”Pick<IssuedSession<unknown>, "cookie" | "deviceCookie" | "session">
Returns
Section titled “Returns”object
csrfToken
Section titled “csrfToken”csrfToken:
string
signOut()
Section titled “signOut()”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.
Returns
Section titled “Returns”Promise<{ csrfToken: string; }>
user()
Section titled “user()”user():
Promise<U|null>
The session’s user, loaded from the store once per request; null when signed out or deleted.
Returns
Section titled “Returns”Promise<U | null>