import Cookies from 'js-cookie' import { create } from 'zustand' const ACCESS_TOKEN = 'thisisjustarandomstring' interface AuthUser { accountNo: string email: string role: string[] exp: number } interface AuthState { auth: { user: AuthUser | null setUser: (user: AuthUser | null) => void accessToken: string setAccessToken: (accessToken: string) => void resetAccessToken: () => void reset: () => void } } export const useAuthStore = create()((set) => { const cookieState = Cookies.get(ACCESS_TOKEN) const initToken = cookieState ? JSON.parse(cookieState) : '' return { auth: { user: null, setUser: (user) => set((state) => ({ ...state, auth: { ...state.auth, user } })), accessToken: initToken, setAccessToken: (accessToken) => set((state) => { Cookies.set(ACCESS_TOKEN, JSON.stringify(accessToken)) return { ...state, auth: { ...state.auth, accessToken } } }), resetAccessToken: () => set((state) => { Cookies.remove(ACCESS_TOKEN) return { ...state, auth: { ...state.auth, accessToken: '' } } }), reset: () => set((state) => { Cookies.remove(ACCESS_TOKEN) return { ...state, auth: { ...state.auth, user: null, accessToken: '' }, } }), }, } }) // export const useAuth = () => useAuthStore((state) => state.auth)