fix: apply bugfix agent improvements to src/store/authStore.ts
This commit is contained in:
parent
b1c76c7f86
commit
9088b06ec5
|
|
@ -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,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
Loading…
Reference in New Issue