From 6008aa9b042e66da8edf4b6a10795dd930e70dca Mon Sep 17 00:00:00 2001 From: zetaloop Date: Fri, 20 Feb 2026 20:40:25 +0800 Subject: [PATCH] fix: improve auth store with user state and role validation --- app/(auth)/register/page.tsx | 3 ++- components/login-dialog.tsx | 3 ++- lib/mock-data.ts | 18 +++++++++--------- store/auth.ts | 31 +++++++++++++++++++++++++------ 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/app/(auth)/register/page.tsx b/app/(auth)/register/page.tsx index cc32ed3..0af97fa 100644 --- a/app/(auth)/register/page.tsx +++ b/app/(auth)/register/page.tsx @@ -9,6 +9,7 @@ import { z } from "zod" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" +import { currentUser } from "@/lib/mock-data" import { useAuthStore } from "@/store/auth" const registerSchema = z @@ -36,7 +37,7 @@ export default function RegisterPage() { const onSubmit = async (_data: z.infer) => { await new Promise((r) => setTimeout(r, 500)) - login() + login(currentUser, ["consumer"]) router.push("/") } diff --git a/components/login-dialog.tsx b/components/login-dialog.tsx index 1ea657f..17bc32b 100644 --- a/components/login-dialog.tsx +++ b/components/login-dialog.tsx @@ -14,6 +14,7 @@ import { } from "@/components/ui/dialog" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" +import { currentUser } from "@/lib/mock-data" import { useAuthStore } from "@/store/auth" const loginSchema = z.object({ @@ -39,7 +40,7 @@ export function LoginDialog({ open, onOpenChange }: LoginDialogProps) { const onSubmit = async (_data: z.infer) => { await new Promise((r) => setTimeout(r, 500)) - login() + login(currentUser, ["consumer", "player", "owner"]) onOpenChange(false) } diff --git a/lib/mock-data.ts b/lib/mock-data.ts index 2544362..69725bf 100644 --- a/lib/mock-data.ts +++ b/lib/mock-data.ts @@ -39,7 +39,7 @@ export const mockUsers: User[] = [ }, { id: "u3", - username: "wang-ge01", + username: "wangzai77", nickname: "王哥", avatar: "/avatars/u3.jpg", role: "owner", @@ -135,7 +135,7 @@ export const mockServices: PlayerService[] = [ description: "每日委托、树脂、活动本都能清。做完发截图,不乱动你号。", price: 19, unit: "次", - availability: ["工作日 21:00-24:00", "周末白天可"], + availability: ["工作日 21:00-24:00", "周末白天可加急"], }, ] @@ -278,7 +278,7 @@ export const mockOrders: Order[] = [ shopName: "王者电竞工作室", service: mockServices[1], status: "completed", - totalPrice: 58, + totalPrice: 54, note: "补两星就收工", createdAt: "2025-02-08T21:10:00", acceptedAt: "2025-02-08T21:15:00", @@ -295,7 +295,7 @@ export const mockOrders: Order[] = [ shopName: "王者电竞工作室", service: mockServices[4], status: "completed", - totalPrice: 34, + totalPrice: 38, note: "帮我清两天日常", createdAt: "2025-02-06T19:40:00", acceptedAt: "2025-02-06T19:45:00", @@ -310,7 +310,7 @@ export const mockOrders: Order[] = [ playerName: "阿辰", service: mockServices[2], status: "completed", - totalPrice: 58, + totalPrice: 57, createdAt: "2025-02-03T23:10:00", acceptedAt: "2025-02-03T23:12:00", closedAt: "2025-02-04T00:05:00", @@ -695,28 +695,28 @@ export const mockTransactions: WalletTransaction[] = [ { id: "t7", type: "payment", - amount: -58, + amount: -54, description: "支付订单 ord5", createdAt: "2025-02-08T21:10:00", }, { id: "t8", type: "payment", - amount: -34, + amount: -38, description: "支付订单 ord6", createdAt: "2025-02-06T19:40:00", }, { id: "t9", type: "payment", - amount: -58, + amount: -57, description: "支付订单 ord7", createdAt: "2025-02-03T23:10:00", }, ] export const currentUser = mockUsers[0] -export const walletBalance = 179 +export const walletBalance = 180 export const mockFavorites: Favorite[] = [ { diff --git a/store/auth.ts b/store/auth.ts index 97acc69..54e1e84 100644 --- a/store/auth.ts +++ b/store/auth.ts @@ -1,20 +1,39 @@ import { create } from "zustand" -import type { UserRole } from "@/lib/types" +import type { User, UserRole } from "@/lib/types" interface AuthState { isAuthenticated: boolean currentRole: UserRole verifiedRoles: UserRole[] + user: User | null switchRole: (role: UserRole) => void - login: () => void + login: (user: User, verifiedRoles?: UserRole[]) => void logout: () => void } -export const useAuthStore = create((set) => ({ +export const useAuthStore = create((set, get) => ({ isAuthenticated: false, currentRole: "consumer", verifiedRoles: ["consumer"], - switchRole: (role) => set({ currentRole: role }), - login: () => set({ isAuthenticated: true }), - logout: () => set({ isAuthenticated: false, currentRole: "consumer" }), + user: null, + switchRole: (role) => { + const { verifiedRoles } = get() + if (verifiedRoles.includes(role)) { + set({ currentRole: role }) + } + }, + login: (user, verifiedRoles = ["consumer"]) => + set({ + isAuthenticated: true, + user, + currentRole: user.role, + verifiedRoles, + }), + logout: () => + set({ + isAuthenticated: false, + currentRole: "consumer", + verifiedRoles: ["consumer"], + user: null, + }), }))