You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
703 B
TypeScript

import { TRPCError } from '@trpc/server';
import { enableClerk } from '@/const/auth';
import { trpc } from '../init';
export const userAuth = trpc.middleware(async (opts) => {
const { ctx } = opts;
// `ctx.user` is nullable
console.log(ctx.userId,'1111111111111111userId-------------')
if (!ctx.userId) {
if (enableClerk) {
console.log('clerk auth:', ctx.clerkAuth);
} else {
console.log('next auth:', ctx.nextAuth);
}
console.log(ctx.userId,'2222222222222222userId-------------')
throw new TRPCError({ code: 'UNAUTHORIZED' });
}
return opts.next({
ctx: {
// ✅ user value is known to be non-null now
userId: ctx.userId,
},
});
});