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,27 @@
|
||||
export const AVATARS = [
|
||||
{ emoji: "🐱", bg: "bg-amber-100" },
|
||||
{ emoji: "🐶", bg: "bg-orange-100" },
|
||||
{ emoji: "🦊", bg: "bg-red-100" },
|
||||
{ emoji: "🐰", bg: "bg-pink-100" },
|
||||
{ emoji: "🐼", bg: "bg-zinc-100" },
|
||||
{ emoji: "🐨", bg: "bg-sky-100" },
|
||||
{ emoji: "🦁", bg: "bg-yellow-100" },
|
||||
{ emoji: "🐸", bg: "bg-lime-100" },
|
||||
{ emoji: "🐵", bg: "bg-stone-100" },
|
||||
{ emoji: "🐷", bg: "bg-rose-100" },
|
||||
{ emoji: "🐙", bg: "bg-purple-100" },
|
||||
{ emoji: "🦄", bg: "bg-violet-100" },
|
||||
] as const;
|
||||
|
||||
export function getAvatar(uid: string) {
|
||||
let hash = 0;
|
||||
for (let i = 0; i < uid.length; i++) {
|
||||
hash = (hash * 31 + uid.charCodeAt(i)) | 0;
|
||||
}
|
||||
return AVATARS[((hash % AVATARS.length) + AVATARS.length) % AVATARS.length];
|
||||
}
|
||||
|
||||
export function getAvatarBg(emoji: string): string {
|
||||
const found = AVATARS.find((a) => a.emoji === emoji);
|
||||
return found?.bg ?? "bg-zinc-100";
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { getRoomData } from "./store";
|
||||
import type { RoomStatus, MatchType } from "@/types";
|
||||
import { prisma } from "./prisma";
|
||||
import type { RoomStatus, MatchType, UserProfile } from "@/types";
|
||||
|
||||
export async function buildRoomStatus(
|
||||
roomId: string,
|
||||
@@ -41,6 +42,17 @@ export async function buildRoomStatus(
|
||||
}
|
||||
}
|
||||
|
||||
const userProfiles: Record<string, UserProfile> = {};
|
||||
if (data.users.length > 0) {
|
||||
const dbUsers = await prisma.user.findMany({
|
||||
where: { id: { in: data.users } },
|
||||
select: { id: true, username: true, avatar: true },
|
||||
});
|
||||
for (const u of dbUsers) {
|
||||
userProfiles[u.id] = { id: u.id, username: u.username, avatar: u.avatar };
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
roomId,
|
||||
userCount: data.users.length,
|
||||
@@ -54,6 +66,7 @@ export async function buildRoomStatus(
|
||||
creatorId: data.creatorId,
|
||||
locked: data.locked,
|
||||
users: data.users,
|
||||
userProfiles,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import type { UserProfile, UserPreferences } from "@/types";
|
||||
|
||||
const STORAGE_KEY = "nowhatever_user_id";
|
||||
const PROFILE_KEY = "nowhatever_profile";
|
||||
|
||||
export function getUserId(): string {
|
||||
if (typeof window === "undefined") return "";
|
||||
@@ -10,3 +13,51 @@ export function getUserId(): string {
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
export function getCachedProfile(): UserProfile | null {
|
||||
if (typeof window === "undefined") return null;
|
||||
try {
|
||||
const raw = localStorage.getItem(PROFILE_KEY);
|
||||
return raw ? JSON.parse(raw) : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function setCachedProfile(profile: UserProfile | null): void {
|
||||
if (typeof window === "undefined") return;
|
||||
if (profile) {
|
||||
localStorage.setItem(PROFILE_KEY, JSON.stringify(profile));
|
||||
} else {
|
||||
localStorage.removeItem(PROFILE_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
export function isRegistered(): boolean {
|
||||
return getCachedProfile() !== null;
|
||||
}
|
||||
|
||||
export function getCachedPreferences(): UserPreferences {
|
||||
if (typeof window === "undefined") return {};
|
||||
try {
|
||||
const profile = getCachedProfile();
|
||||
if (!profile) return {};
|
||||
const raw = localStorage.getItem("nowhatever_preferences");
|
||||
return raw ? JSON.parse(raw) : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export function setCachedPreferences(prefs: UserPreferences): void {
|
||||
if (typeof window === "undefined") return;
|
||||
localStorage.setItem("nowhatever_preferences", JSON.stringify(prefs));
|
||||
}
|
||||
|
||||
export function logout(): void {
|
||||
if (typeof window === "undefined") return;
|
||||
localStorage.removeItem(PROFILE_KEY);
|
||||
localStorage.removeItem("nowhatever_preferences");
|
||||
const newId = crypto.randomUUID();
|
||||
localStorage.setItem(STORAGE_KEY, newId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user