aboutsummaryrefslogtreecommitdiffstats
path: root/packages/web/server/api/trpc.ts
blob: 7c4af4527e0c032ff6e94c877979b119c63efd0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { TRPCError, initTRPC } from "@trpc/server";
import { User } from "next-auth";
import superjson from "superjson";

export type Context = {
  user: User | null;
};

// Avoid exporting the entire t-object
// since it's not very descriptive.
// For instance, the use of a t variable
// is common in i18n libraries.
const t = initTRPC.context<Context>().create({
  transformer: superjson,
});
export const createCallerFactory = t.createCallerFactory;
// Base router and procedure helpers
export const router = t.router;
export const procedure = t.procedure;
export const publicProcedure = t.procedure;

export const authedProcedure = t.procedure.use(function isAuthed(opts) {
  const user = opts.ctx.user;

  if (!user) {
    throw new TRPCError({ code: "UNAUTHORIZED" });
  }

  return opts.next({
    ctx: {
      user,
    },
  });
});