Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

trpc.ts

28 lines665 Bytes

1import { initTRPC, TRPCError } from "@trpc/server"
2import { auth } from "@/lib/auth"
3import { db } from "@/db/connection"
4​
5export type Context = {
6 session: Awaited<ReturnType<typeof auth.api.getSession>>
7 db: typeof db
8}
9​
10const t = initTRPC.context<Context>().create()
11​
12export const router = t.router
13export const procedure = t.procedure
14export const middleware = t.middleware
15​
16export const isAuthed = middleware(async ({ ctx, next }) => {
17 if (!ctx.session?.user) {
18 throw new TRPCError({ code: "UNAUTHORIZED" })
19 }
20 return next({
21 ctx: {
22 ...ctx,
23 session: ctx.session,
24 },
25 })
26})
27​
28export const authedProcedure = procedure.use(isAuthed)