Spaces:
Sleeping
Sleeping
File size: 1,261 Bytes
d34aa41 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import { initTRPC, TRPCError } from '@trpc/server';
import superjson from 'superjson';
import type { TRPCContext } from './context';
const t = initTRPC.context<TRPCContext>().create({
transformer: superjson,
});
const requireUser = t.middleware(({ ctx, next }) => {
if (!ctx.session?.user) {
throw new TRPCError({ code: 'UNAUTHORIZED' });
}
return next({
ctx: {
...ctx,
user: ctx.session.user,
},
});
});
const requireAdmin = t.middleware(({ ctx, next }) => {
const user = ctx.session?.user;
if (user?.role !== 'ADMIN') {
throw new TRPCError({ code: 'FORBIDDEN' });
}
return next({
ctx: {
...ctx,
user,
},
});
});
const requireMuhasebe = t.middleware(({ ctx, next }) => {
const user = ctx.session?.user;
const role = user?.role;
if (role !== 'ADMIN' && role !== 'MUHASEBE') {
throw new TRPCError({ code: 'FORBIDDEN' });
}
return next({
ctx: {
...ctx,
user,
},
});
});
export const createTRPCRouter = t.router;
export const publicProcedure = t.procedure;
export const protectedProcedure = t.procedure.use(requireUser);
export const adminProcedure = t.procedure.use(requireAdmin);
export const muhasebeProcedure = t.procedure.use(requireMuhasebe);
|