Skip to content

AuthMiddlewares

TContext

TMeta

TErrorShape

TTier extends string = never

readonly authed: MiddlewareBuilder<TContext, TMeta, PrincipalOf<TContext>, unknown>

Refuses a forged mutation (403 forbidden), then a caller without a valid session (401 session_expired); adds user and session.


readonly csrf: MiddlewareBuilder<TContext, TMeta, object, unknown>

Refuses a mutation that fails the CSRF checks (403 forbidden).


readonly protectedProcedure: AuthProcedureBuilder<TContext, TMeta, PrincipalOf<TContext>, TErrorShape>

CSRF on mutations and a valid session, with ctx.user and ctx.session.


readonly publicProcedure: AuthProcedureBuilder<TContext, TMeta, object, TErrorShape>

Marked public: CSRF on mutations, no session needed (sign-in, sign-up, reset).


readonly writesCookies: MiddlewareBuilder<TContext, TMeta, WriterOf<TContext>, unknown>

The only way to the cookie writers: after it, ctx.auth is an AuthWriter with setSession, signOut and clearSession; without it they are not on the type and refuse at run time. Only a mutation may carry it: on a query (no CSRF check, and reachable by a cross-site GET) or a subscription it refuses every call as a configuration error. It runs the CSRF check itself (403 forbidden), so a forged mutation never reaches the writers, even on a bare t.procedure. On a streamed batch (httpBatchStreamLink), whose headers are already sent, it refuses before the procedure runs, so a sign-in or a rotation never happens without its cookie.

freshAuth(maxAge?): MiddlewareBuilder<TContext, TMeta, PrincipalOf<TContext>, unknown>

Requires a recent credential proof (403 fresh_auth_required); runs the authed check itself. maxAge can only shorten freshAuthAge.

Duration

MiddlewareBuilder<TContext, TMeta, PrincipalOf<TContext>, unknown>


rateLimited(name, rule?): MiddlewareBuilder<TContext, TMeta, object, unknown>

A fixed window under trpc:<name> (429 rate_limited), per client IP unless the rule says keyBy: 'user'. The rule is rule when given (a tier name, or a rule of its own), else the tier named name, else the default tier, else 10 a minute. Skipped on a procedure whose meta says rateLimit: 'exempt'. Refuses when the counter store is down.

string

TrpcRateLimitRule | TTier

MiddlewareBuilder<TContext, TMeta, object, unknown>


role(…roles): MiddlewareBuilder<TContext, TMeta, PrincipalOf<TContext>, unknown>

Requires one of roles (403 forbidden); runs the authed check itself.

…readonly string[]

MiddlewareBuilder<TContext, TMeta, PrincipalOf<TContext>, unknown>