interface Meta {
role: Role;
}
const t = initTRPC.context<Context>().meta<Meta>().create({
defaultMeta: {
role: Role.OrganizationUser
}
});
export const authRoleProcedure = t.procedure.use(async (opts) => {
const { meta, next, ctx } = opts;
if (!meta?.role) {
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message: 'Role not defined in meta' });
}
if (!ctx.user) {
throw new TRPCError({ code: 'BAD_REQUEST', message: 'User not defined in context'});
}
if (ctx.user.role > meta.role) {
throw new TRPCError({ code: 'UNAUTHORIZED', message: `User does not have the required role. Required: ${meta.role}, User: ${ctx.user.role}`});
}
console.log('User has the required role')
return next();
});
export const router = t.router;
interface Meta {
role: Role;
}
const t = initTRPC.context<Context>().meta<Meta>().create({
defaultMeta: {
role: Role.OrganizationUser
}
});
export const authRoleProcedure = t.procedure.use(async (opts) => {
const { meta, next, ctx } = opts;
if (!meta?.role) {
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message: 'Role not defined in meta' });
}
if (!ctx.user) {
throw new TRPCError({ code: 'BAD_REQUEST', message: 'User not defined in context'});
}
if (ctx.user.role > meta.role) {
throw new TRPCError({ code: 'UNAUTHORIZED', message: `User does not have the required role. Required: ${meta.role}, User: ${ctx.user.role}`});
}
console.log('User has the required role')
return next();
});
export const router = t.router;