mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
25 lines
543 B
TypeScript
25 lines
543 B
TypeScript
import { initTRPC, TRPCError } from "@trpc/server";
|
|
import type { Context } from "./context";
|
|
|
|
export const t = initTRPC.context<Context>().create();
|
|
|
|
export const router = t.router;
|
|
|
|
export const publicProcedure = t.procedure;
|
|
|
|
export const protectedProcedure = t.procedure.use(({ ctx, next }) => {
|
|
if (!ctx.session) {
|
|
throw new TRPCError({
|
|
code: "UNAUTHORIZED",
|
|
message: "Authentication required",
|
|
cause: "No session",
|
|
});
|
|
}
|
|
return next({
|
|
ctx: {
|
|
...ctx,
|
|
session: ctx.session,
|
|
},
|
|
});
|
|
});
|