AuthContextFactory
@noctcore/auth-trpc: tRPC middlewares for @noctcore/auth.
createAuthContextchecks the options once and returns the context creators for the fetch and Node adapters:ctx.authholds the session the core accepted, the request meta and the cookie writers.createAuthMiddlewares(t)returnscsrf,authed,freshAuth,role,rateLimitedandwritesCookies, pluspublicProcedure(CSRF on mutations) andprotectedProcedure(CSRF and a valid session).assertProceduresGuarded(router)fails on a procedure built with neither.authResponseMeta(responseMeta)keepsCache-Control: no-storeon a response that handed out a CSRF token, whatever the app’s ownresponseMetasets.- The nestjs-trpc classes run the same bodies;
AuthGuardMiddlewareis the closed-by-default global for nestjs-trpc. toTRPCErrorandauthErrorFormattermap refusals to tRPC errors without leaking account state, and keep stacks, submitted values and internal messages out of the errors tRPC and the adapter raise.- Zod input and output schemas for the auth procedures, and
assertNoSecretQueriesto keep secrets out of URLs.
Type Parameters
Section titled “Type Parameters”U extends AuthUser
A = unknown
Methods
Section titled “Methods”fetch()
Section titled “fetch()”fetch(
input,peer?):Promise<{auth:AuthState<U,A>; }>
For the fetch adapter: createContext: (opts) => authContext.fetch(opts).
A fetch Request carries no peer address, so pass it from your server
(Bun: server.requestIP(req)?.address); without it, and with no trusted
hops, every client shares the IP unknown.
Parameters
Section titled “Parameters”remoteAddress?
Section titled “remoteAddress?”string
Returns
Section titled “Returns”Promise<{ auth: AuthState<U, A>; }>
node()
Section titled “node()”node(
input):Promise<{auth:AuthState<U,A>; }>
For the Node adapters (standalone, Express, nestjs-trpc): createContext: (opts) => authContext.node(opts).
The WebSocket and Fastify adapters are not supported yet: a res that
cannot set headers (the socket, a Fastify reply) is refused with an
AuthConfigError.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”Promise<{ auth: AuthState<U, A>; }>