Skip to content

Commit

Permalink
feat: create admin procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrastopoulos committed Apr 6, 2024
1 parent 13a4292 commit 991d67f
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions packages/api/src/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,7 @@ export const createTRPCRouter = t.router;
*/
export const publicProcedure = t.procedure;

/**
* Protected (authenticated) procedure
*
* If you want a query or mutation to ONLY be accessible to logged in users, use this. It verifies
* the session is valid and guarantees `ctx.session.user` is not null.
*
* @see https://trpc.io/docs/procedures
*/
export const protectedProcedure = t.procedure.use(({ ctx, next }) => {
const protectedMiddleware = t.middleware(({ ctx, next }) => {
if (!ctx.session?.user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
Expand All @@ -107,3 +99,23 @@ export const protectedProcedure = t.procedure.use(({ ctx, next }) => {
},
});
});

/**
* Protected (authenticated) procedure
*
* If you want a query or mutation to ONLY be accessible to logged in users, use this. It verifies
* the session is valid and guarantees `ctx.session.user` is not null.
*
* @see https://trpc.io/docs/procedures
*/
export const protectedProcedure = t.procedure.use(protectedMiddleware);

export const adminProcedure = protectedMiddleware.unstable_pipe(
({ ctx, next }) => {
// if (!ctx.session?.user?.isAdmin) {
// throw new TRPCError({ code: "UNAUTHORIZED" });
// }

return next({ ctx });
},
);

0 comments on commit 991d67f

Please sign in to comment.