AuthMiddlewares
Type Parameters
Section titled “Type Parameters”TContext
Section titled “TContext”TContext
TMeta
TErrorShape
Section titled “TErrorShape”TErrorShape
TTier extends string = never
Properties
Section titled “Properties”authed
Section titled “authed”
readonlyauthed: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.
readonlycsrf:MiddlewareBuilder<TContext,TMeta,object,unknown>
Refuses a mutation that fails the CSRF checks (403 forbidden).
protectedProcedure
Section titled “protectedProcedure”
readonlyprotectedProcedure:AuthProcedureBuilder<TContext,TMeta,PrincipalOf<TContext>,TErrorShape>
CSRF on mutations and a valid session, with ctx.user and ctx.session.
publicProcedure
Section titled “publicProcedure”
readonlypublicProcedure:AuthProcedureBuilder<TContext,TMeta,object,TErrorShape>
Marked public: CSRF on mutations, no session needed (sign-in, sign-up, reset).
writesCookies
Section titled “writesCookies”
readonlywritesCookies: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.
Methods
Section titled “Methods”freshAuth()
Section titled “freshAuth()”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.
Parameters
Section titled “Parameters”maxAge?
Section titled “maxAge?”Duration
Returns
Section titled “Returns”MiddlewareBuilder<TContext, TMeta, PrincipalOf<TContext>, unknown>
rateLimited()
Section titled “rateLimited()”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.
Parameters
Section titled “Parameters”string
TrpcRateLimitRule | TTier
Returns
Section titled “Returns”MiddlewareBuilder<TContext, TMeta, object, unknown>
role()
Section titled “role()”role(…
roles):MiddlewareBuilder<TContext,TMeta,PrincipalOf<TContext>,unknown>
Requires one of roles (403 forbidden); runs the authed check itself.
Parameters
Section titled “Parameters”…readonly string[]
Returns
Section titled “Returns”MiddlewareBuilder<TContext, TMeta, PrincipalOf<TContext>, unknown>