cal.pub0.org/server/createRouter.ts

27 lines
622 B
TypeScript
Raw Normal View History

import * as trpc from "@trpc/server";
import { Context } from "./createContext";
/**
* Helper function to create a router with context
*/
export function createRouter() {
return trpc.router<Context>();
}
export function createProtectedRouter() {
return createRouter().middleware(({ ctx, next }) => {
if (!ctx.user || !ctx.session) {
throw new trpc.TRPCError({ code: "UNAUTHORIZED" });
}
return next({
ctx: {
...ctx,
// infers that `user` and `session` are non-nullable to downstream procedures
session: ctx.session,
user: ctx.user,
},
});
});
}