import { create } from "zustand"; import { ABILITIES_KEY, TOKEN_KEY, USER_KEY } from "../config/AppKey"; interface AuthStore { token: string | null | undefined; abilities: any; isAuthenticated: boolean; login: (Data: any) => Promise; logout: () => Promise; } const useAuthState = create((set) => { const storedToken = localStorage.getItem(TOKEN_KEY); const storedAbilities = localStorage.getItem(ABILITIES_KEY); return { isAuthenticated: !!storedToken, token: storedToken, abilities: storedAbilities, login: async (Data) => { console.log(Data); localStorage.setItem(TOKEN_KEY, Data?.token); localStorage.setItem(USER_KEY, JSON.stringify(Data?.user)); localStorage.setItem(ABILITIES_KEY, JSON.stringify(Data?.abilities)); set((state) => ({ isAuthenticated: true, token: Data?.token, abilities: Data?.abilities, })); }, logout: async () => { localStorage.removeItem(TOKEN_KEY); localStorage.removeItem(USER_KEY); localStorage.removeItem(ABILITIES_KEY); set(() => ({ isAuthenticated: false, token: null, abilities: null, })); }, }; }); export default useAuthState;