feat: 用户名密码登录注册系统
- 新增 /api/auth/register 和 /api/auth/login 接口,使用 bcryptjs 哈希密码 - User 模型改为 username + passwordHash,id 自动生成 cuid - 新增 AuthModal 组件(登录/注册双标签页),替换旧的 ProfileSetupModal - 重写 /profile 页面:支持修改用户名、密码、头像、绑定邮箱、退出登录 - /api/user PUT 支持密码修改(需验证当前密码)和用户名唯一性校验 - 游客模式保留,右上角显示"登录"按钮;登录后显示头像和用户名 - 全局 nickname -> username 重命名(types、SwipeDeck、RoomManageModal、buildRoomStatus) - 新增 logout() 清除登录态并重新生成游客 UUID
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const userId = req.nextUrl.searchParams.get("userId");
|
||||
if (!userId) {
|
||||
return NextResponse.json([]);
|
||||
}
|
||||
|
||||
const favorites = await prisma.favorite.findMany({
|
||||
where: { userId },
|
||||
orderBy: { createdAt: "desc" },
|
||||
take: 50,
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
favorites.map((f) => ({
|
||||
id: f.id,
|
||||
restaurantData: JSON.parse(f.restaurantData),
|
||||
createdAt: f.createdAt.toISOString(),
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const { userId, restaurant } = await req.json();
|
||||
|
||||
if (!userId || !restaurant) {
|
||||
return NextResponse.json({ error: "缺少必要字段" }, { status: 400 });
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({ where: { id: userId } });
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "请先设置个人资料" }, { status: 404 });
|
||||
}
|
||||
|
||||
const existing = await prisma.favorite.findFirst({
|
||||
where: {
|
||||
userId,
|
||||
restaurantData: { contains: `"id":"${restaurant.id}"` },
|
||||
},
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
return NextResponse.json({ id: existing.id, alreadyExists: true });
|
||||
}
|
||||
|
||||
const fav = await prisma.favorite.create({
|
||||
data: {
|
||||
userId,
|
||||
restaurantData: JSON.stringify(restaurant),
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ id: fav.id });
|
||||
}
|
||||
|
||||
export async function DELETE(req: NextRequest) {
|
||||
const { userId, favoriteId } = await req.json();
|
||||
|
||||
if (!userId || !favoriteId) {
|
||||
return NextResponse.json({ error: "缺少必要字段" }, { status: 400 });
|
||||
}
|
||||
|
||||
const fav = await prisma.favorite.findUnique({ where: { id: favoriteId } });
|
||||
if (!fav || fav.userId !== userId) {
|
||||
return NextResponse.json({ error: "收藏不存在" }, { status: 404 });
|
||||
}
|
||||
|
||||
await prisma.favorite.delete({ where: { id: favoriteId } });
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
Reference in New Issue
Block a user