2021-09-27 14:47:55 +00:00
|
|
|
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>();
|
|
|
|
}
|
2021-09-28 08:57:30 +00:00
|
|
|
|
|
|
|
export function createProtectedRouter() {
|
|
|
|
return createRouter().middleware(({ ctx, next }) => {
|
2021-10-14 10:57:49 +00:00
|
|
|
if (!ctx.user || !ctx.session) {
|
2021-09-28 08:57:30 +00:00
|
|
|
throw new trpc.TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
}
|
|
|
|
return next({
|
|
|
|
ctx: {
|
|
|
|
...ctx,
|
2021-10-14 10:57:49 +00:00
|
|
|
// infers that `user` and `session` are non-nullable to downstream procedures
|
|
|
|
session: ctx.session,
|
2021-09-28 08:57:30 +00:00
|
|
|
user: ctx.user,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|