From 9088b06ec5b8b49c162c7fd7b6a7126d9549b6e3 Mon Sep 17 00:00:00 2001 From: cupadev-admin Date: Mon, 9 Mar 2026 12:48:39 +0000 Subject: [PATCH] fix: apply bugfix agent improvements to src/store/authStore.ts --- src/store/authStore.ts | 58 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/store/authStore.ts diff --git a/src/store/authStore.ts b/src/store/authStore.ts new file mode 100644 index 0000000..14f3fc1 --- /dev/null +++ b/src/store/authStore.ts @@ -0,0 +1,58 @@ +import { create } from "zustand"; +import { persist } from "zustand/middleware"; +import type { AuthState, User } from "@/types"; + +const DEFAULT_USER: User = { + id: "1", + name: "Admin", + email: "admin@cms.dev", + password: "admin123", +}; + +export const useAuthStore = create()( + persist( + (set, get) => ({ + user: null, + isAuthenticated: false, + + login: (email: string, password: string): boolean => { + const stored = get().user; + const target = stored ?? DEFAULT_USER; + + if ( + email === target.email && + password === (target.password ?? "admin123") + ) { + set({ + user: { ...target, password: target.password ?? "admin123" }, + isAuthenticated: true, + }); + return true; + } + return false; + }, + + logout: () => { + set({ isAuthenticated: false }); + }, + + checkAuth: () => { + // Handled by persist middleware rehydration + // No-op since persist restores state automatically + }, + + updateUser: (data: Partial) => { + set((state) => ({ + user: state.user ? { ...state.user, ...data } : state.user, + })); + }, + }), + { + name: "cms-auth", + partialize: (state) => ({ + user: state.user, + isAuthenticated: state.isAuthenticated, + }), + } + ) +);