Skip to content

AuthContextFactory

@noctcore/auth-trpc: tRPC middlewares for @noctcore/auth.

  • createAuthContext checks the options once and returns the context creators for the fetch and Node adapters: ctx.auth holds the session the core accepted, the request meta and the cookie writers.
  • createAuthMiddlewares(t) returns csrf, authed, freshAuth, role, rateLimited and writesCookies, plus publicProcedure (CSRF on mutations) and protectedProcedure (CSRF and a valid session).
  • assertProceduresGuarded(router) fails on a procedure built with neither.
  • authResponseMeta(responseMeta) keeps Cache-Control: no-store on a response that handed out a CSRF token, whatever the app’s own responseMeta sets.
  • The nestjs-trpc classes run the same bodies; AuthGuardMiddleware is the closed-by-default global for nestjs-trpc.
  • toTRPCError and authErrorFormatter map 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 assertNoSecretQueries to keep secrets out of URLs.

U extends AuthUser

A = unknown

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.

FetchContextInput

string

Promise<{ auth: AuthState<U, A>; }>


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.

NodeContextInput

Promise<{ auth: AuthState<U, A>; }>