Add board customization page

This commit is contained in:
Meier Lukas
2023-07-31 11:15:18 +02:00
parent e448ce4b00
commit 130b51e109
7 changed files with 267 additions and 64 deletions

View File

@@ -128,3 +128,30 @@ const enforceUserIsAuthed = t.middleware(({ ctx, next }) => {
* @see https://trpc.io/docs/procedures
*/
export const protectedProcedure = t.procedure.use(enforceUserIsAuthed);
/** Reusable middleware that enforces users are logged in before running the procedure. */
const enforceUserIsAdmin = t.middleware(({ ctx, next }) => {
if (!ctx.session?.user) {
throw new TRPCError({ code: 'UNAUTHORIZED' });
}
if (!ctx.session?.user.isAdmin) {
throw new TRPCError({ code: 'FORBIDDEN' });
}
return next({
ctx: {
// infers the `session` as non-nullable
session: { ...ctx.session, user: ctx.session.user },
},
});
});
/**
* Admin (authenticated) procedure
*
* If you want a query or mutation to ONLY be accessible to logged in admins, use this. It verifies
* the session is valid, guarantees `ctx.session.user` is not null and the user is an admin.
*
* @see https://trpc.io/docs/procedures
*/
export const adminProcedure = t.procedure.use(enforceUserIsAdmin);