fix(聊天): 添加用户认证和 API Key 解密

- 聊天前检查用户登录状态
- 获取当前用户的设置和 API Key
- 使用时解密 API Key
- 优化未配置 API Key 的错误提示
This commit is contained in:
gaoziman 2025-12-21 14:04:01 +08:00
parent 058ea85daa
commit fd6c93cb30

View File

@ -4,6 +4,8 @@ import { conversations, messages, userSettings } from '@/drizzle/schema';
import { eq } from 'drizzle-orm'; import { eq } from 'drizzle-orm';
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
import { executeTool } from '@/services/tools'; import { executeTool } from '@/services/tools';
import { getCurrentUser } from '@/lib/auth';
import { decryptApiKey } from '@/lib/crypto';
interface ChatRequest { interface ChatRequest {
conversationId: string; conversationId: string;
@ -212,18 +214,30 @@ export async function POST(request: Request) {
})) : undefined, })) : undefined,
}); });
// 获取用户设置 // 获取当前登录用户
const user = await getCurrentUser();
if (!user) {
return NextResponse.json(
{ error: '请先登录后再使用聊天功能' },
{ status: 401 }
);
}
// 获取该用户的设置
const settings = await db.query.userSettings.findFirst({ const settings = await db.query.userSettings.findFirst({
where: eq(userSettings.id, 1), where: eq(userSettings.userId, user.userId),
}); });
if (!settings?.cchApiKey) { if (!settings?.cchApiKey) {
return NextResponse.json( return NextResponse.json(
{ error: 'Please configure your CCH API key in settings' }, { error: '请先在设置中配置您的 API Key 才能使用聊天功能', code: 'API_KEY_NOT_CONFIGURED' },
{ status: 400 } { status: 400 }
); );
} }
// 解密 API Key
const decryptedApiKey = decryptApiKey(settings.cchApiKey);
// 获取对话信息 // 获取对话信息
const conversation = await db.query.conversations.findFirst({ const conversation = await db.query.conversations.findFirst({
where: eq(conversations.conversationId, conversationId), where: eq(conversations.conversationId, conversationId),
@ -267,7 +281,7 @@ export async function POST(request: Request) {
const stream = new ReadableStream({ const stream = new ReadableStream({
async start(controller) { async start(controller) {
try { try {
const cchUrl = settings.cchUrl || 'http://localhost:13500'; const cchUrl = settings.cchUrl || process.env.CCH_DEFAULT_URL || 'https://claude.leocoder.cn/';
// 获取系统提示词(叠加模式) // 获取系统提示词(叠加模式)
// 1. 始终使用 DEFAULT_SYSTEM_PROMPT 作为基础 // 1. 始终使用 DEFAULT_SYSTEM_PROMPT 作为基础
@ -302,7 +316,7 @@ export async function POST(request: Request) {
// ==================== Codex 模型处理OpenAI 格式) ==================== // ==================== Codex 模型处理OpenAI 格式) ====================
const result = await handleCodexChat({ const result = await handleCodexChat({
cchUrl, cchUrl,
apiKey: settings.cchApiKey!, apiKey: decryptedApiKey,
model: useModel, model: useModel,
systemPrompt, systemPrompt,
temperature, temperature,
@ -321,7 +335,7 @@ export async function POST(request: Request) {
// ==================== Claude 模型处理(原有逻辑) ==================== // ==================== Claude 模型处理(原有逻辑) ====================
const result = await handleClaudeChat({ const result = await handleClaudeChat({
cchUrl, cchUrl,
apiKey: settings.cchApiKey!, apiKey: decryptedApiKey,
model: useModel, model: useModel,
systemPrompt, systemPrompt,
temperature, temperature,