@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.
26 lines (18 loc) • 667 B
text/typescript
import { TRPCError } from '@trpc/server';
import { serverDBEnv } from '@/config/db';
import { UserModel } from '@/database/models/user';
import { serverDB } from '@/database/server';
import { asyncTrpc } from './init';
export const asyncAuth = asyncTrpc.middleware(async (opts) => {
const { ctx } = opts;
if (ctx.secret !== serverDBEnv.KEY_VAULTS_SECRET || !ctx.userId) {
throw new TRPCError({ code: 'UNAUTHORIZED' });
}
const result = await UserModel.findById(serverDB, ctx.userId);
if (!result) {
throw new TRPCError({ code: 'UNAUTHORIZED', message: 'user is invalid' });
}
return opts.next({
ctx: { userId: ctx.userId },
});
});