Middlewares

You are able to add middleware(s) to a whole router with the middleware() method. The middleware(s) will wrap the invocation of the procedure and must pass through its return value.

Authorization

In the example below any call to admin.* will ensure that the user is an “admin” before executing any query or mutation.

  1. trpc
  2. .router<Context>()
  3. .query('foo', {
  4. resolve() {
  5. return 'bar';
  6. },
  7. })
  8. .merge(
  9. 'admin.',
  10. trpc
  11. .router<Context>()
  12. .middleware(async ({ ctx, next }) => {
  13. if (!ctx.user?.isAdmin) {
  14. throw new TRPCError({ code: "UNAUTHORIZED" });
  15. }
  16. return next()
  17. })
  18. .query('secretPlace', {
  19. resolve() {
  20. return 'a key';
  21. },
  22. }),
  23. )
tip

See Error Handling to learn more about the TRPCError thrown in the above example.

Logging

In the example below timings for queries are logged automatically.

  1. trpc
  2. .router<Context>()
  3. .middleware(async ({ path, type, next }) => {
  4. const start = Date.now();
  5. const result = await next();
  6. const durationMs = Date.now() - start;
  7. result.ok
  8. ? logMock('OK request timing:', { path, type, durationMs })
  9. : logMock('Non-OK request timing', { path, type, durationMs });
  10. return result;
  11. })
  12. .query('foo', {
  13. resolve() {
  14. return 'bar';
  15. },
  16. })
  17. .query('abc', {
  18. resolve() {
  19. return 'def';
  20. },
  21. })

Context Swapping

A middleware can replace the router’s context, and downstream procedures will receive the new context value:

  1. interface Context {
  2. // user is nullable
  3. user?: {
  4. id: string
  5. }
  6. }
  7. trpc
  8. .router<Context>()
  9. .middleware(({ ctx, next }) => {
  10. if (!ctx.user) {
  11. throw new TRPCError({ code: 'UNAUTHORIZED' });
  12. }
  13. return next({
  14. ctx: {
  15. ...ctx,
  16. user: ctx.user, // user value is known to be non-null now
  17. },
  18. });
  19. })
  20. .query('userId', {
  21. async resolve({ctx}) {
  22. return ctx.user.id;
  23. }
  24. });

createProtectedRouter()-helper

This helper can be used anywhere in your app tree to enforce downstream procedures to be authorized.

server/createRouter.ts

  1. import * as trpc from "@trpc/server";
  2. import { Context } from "./context";
  3. export function createProtectedRouter() {
  4. return trpc
  5. .router<Context>()
  6. .middleware(({ ctx, next }) => {
  7. if (!ctx.user) {
  8. throw new trpc.TRPCError({ code: "UNAUTHORIZED" });
  9. }
  10. return next({
  11. ctx: {
  12. ...ctx,
  13. // infers that `user` is non-nullable to downstream procedures
  14. user: ctx.user,
  15. },
  16. });
  17. });
  18. }

Raw input

A middleware can access the raw input that will be passed to a procedure. This can be used for authentication / other preprocessing in the middleware that requires access to the procedure input, and can be especially useful when used in conjunction with Context Swapping.

caution

The rawInput passed to a middleware has not yet been validated by a procedure’s input schema / validator, so be careful when using it! Because of this, rawInput has type unknown. For more info see #1059.

  1. const inputSchema = z.object({ userId: z.string() });
  2. trpc
  3. .router<Context>()
  4. .middleware(async ({ next, rawInput, ctx }) => {
  5. const result = inputSchema.safeParse(rawInput);
  6. if (!result.success) throw new TRPCError({ code: "BAD_REQUEST" });
  7. const { userId } = result.data;
  8. // Check user id auth
  9. return next({ ctx: { ...ctx, userId }})
  10. })
  11. .query('userId', {
  12. input: inputSchema,
  13. resolve({ ctx }) {
  14. return ctx.userId;
  15. },
  16. });