import { NextRequest, NextResponse } from 'next/server' import { prisma } from '@/lib/prisma' import { verifyPassword, generateToken } from '@/lib/auth' export async function POST(request: NextRequest) { try { const { email, password } = await request.json() if (!email || !password) { return NextResponse.json( { message: '邮箱和密码不能为空' }, { status: 400 } ) } // 查找用户 const user = await prisma.user.findUnique({ where: { email } }) if (!user) { return NextResponse.json( { message: '用户不存在' }, { status: 401 } ) } // 验证密码 const isValidPassword = await verifyPassword(password, user.password) if (!isValidPassword) { return NextResponse.json( { message: '密码错误' }, { status: 401 } ) } // 生成token const token = generateToken({ userId: user.id, email: user.email, isAdmin: user.isAdmin }) // 返回用户信息(不包含密码) const { password: _, ...userWithoutPassword } = user return NextResponse.json({ message: '登录成功', token, user: userWithoutPassword }) } catch (error) { console.error('Login error:', error) return NextResponse.json( { message: '服务器错误' }, { status: 500 } ) } }