@lobehub/chat
Version:
Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.
33 lines (27 loc) • 809 B
text/typescript
import { TRPCError } from '@trpc/server';
import { enableClerk } from '@/const/auth';
import { DESKTOP_USER_ID } from '@/const/desktop';
import { isDesktop } from '@/const/version';
import { trpc } from '../lambda/init';
export const userAuth = trpc.middleware(async (opts) => {
const { ctx } = opts;
// 桌面端模式下,跳过默认鉴权逻辑
if (isDesktop) {
return opts.next({
ctx: { userId: DESKTOP_USER_ID },
});
}
// `ctx.user` is nullable
if (!ctx.userId) {
if (enableClerk) {
console.log('clerk auth:', ctx.clerkAuth);
} else {
console.log('next auth:', ctx.nextAuth);
}
throw new TRPCError({ code: 'UNAUTHORIZED' });
}
return opts.next({
// ✅ user value is known to be non-null now
ctx: { userId: ctx.userId },
});
});