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