mirror of
https://github.com/lukevella/rallly.git
synced 2025-05-12 16:36:49 +02:00
♻️ Create backend package (#643)
This commit is contained in:
parent
7fc08c6736
commit
05fe2edaea
68 changed files with 476 additions and 391 deletions
28
packages/backend/trpc/trpc.ts
Normal file
28
packages/backend/trpc/trpc.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
import { initTRPC, TRPCError } from "@trpc/server";
|
||||
import superjson from "superjson";
|
||||
|
||||
import { Context } from "./context";
|
||||
|
||||
const t = initTRPC.context<Context>().create({
|
||||
transformer: superjson,
|
||||
errorFormatter({ shape }) {
|
||||
return shape;
|
||||
},
|
||||
});
|
||||
|
||||
export const router = t.router;
|
||||
|
||||
export const publicProcedure = t.procedure;
|
||||
|
||||
export const middleware = t.middleware;
|
||||
|
||||
const checkAuthIfRequired = middleware(async ({ ctx, next }) => {
|
||||
if (process.env.AUTH_REQUIRED === "true" && ctx.session) {
|
||||
throw new TRPCError({ code: "UNAUTHORIZED", message: "Login is required" });
|
||||
}
|
||||
return next();
|
||||
});
|
||||
|
||||
export const possiblyPublicProcedure = t.procedure.use(checkAuthIfRequired);
|
||||
|
||||
export const mergeRouters = t.mergeRouters;
|
Loading…
Add table
Add a link
Reference in a new issue