This commit is contained in:
2025-06-22 11:34:32 +08:00
parent 142992a6c9
commit 7d139b22db
55 changed files with 11035 additions and 105 deletions
+59
View File
@@ -0,0 +1,59 @@
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 }
)
}
}
+70
View File
@@ -0,0 +1,70 @@
import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
import { hashPassword } from '@/lib/auth'
export async function POST(request: NextRequest) {
try {
const { email, username, password, name, phone, address } = await request.json()
if (!email || !username || !password) {
return NextResponse.json(
{ message: '邮箱、用户名和密码不能为空' },
{ status: 400 }
)
}
// 检查邮箱是否已存在
const existingUserByEmail = await prisma.user.findUnique({
where: { email }
})
if (existingUserByEmail) {
return NextResponse.json(
{ message: '该邮箱已被注册' },
{ status: 400 }
)
}
// 检查用户名是否已存在
const existingUserByUsername = await prisma.user.findUnique({
where: { username }
})
if (existingUserByUsername) {
return NextResponse.json(
{ message: '该用户名已被使用' },
{ status: 400 }
)
}
// 加密密码
const hashedPassword = await hashPassword(password)
// 创建用户
const user = await prisma.user.create({
data: {
email,
username,
password: hashedPassword,
name: name || null,
phone: phone || null,
address: address || null,
isAdmin: false
}
})
// 返回用户信息(不包含密码)
const { password: _, ...userWithoutPassword } = user
return NextResponse.json({
message: '注册成功',
user: userWithoutPassword
}, { status: 201 })
} catch (error) {
console.error('Register error:', error)
return NextResponse.json(
{ message: '服务器错误' },
{ status: 500 }
)
}
}