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, + }), + } + ) +);