fix: apply bugfix agent improvements to src/store/authStore.ts

This commit is contained in:
cupadev-admin 2026-03-09 12:48:39 +00:00
parent b1c76c7f86
commit 9088b06ec5
1 changed files with 58 additions and 0 deletions

58
src/store/authStore.ts Normal file
View File

@ -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<AuthState>()(
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<User>) => {
set((state) => ({
user: state.user ? { ...state.user, ...data } : state.user,
}));
},
}),
{
name: "cms-auth",
partialize: (state) => ({
user: state.user,
isAuthenticated: state.isAuthenticated,
}),
}
)
);