diff --git a/imago-admin/src/store/auth.store.ts b/imago-admin/src/store/auth.store.ts index 2117a5b..f646deb 100644 --- a/imago-admin/src/store/auth.store.ts +++ b/imago-admin/src/store/auth.store.ts @@ -1,4 +1,5 @@ import { create } from 'zustand'; +import { persist } from 'zustand/middleware'; import { apiGet } from '@/api/client'; import { ENDPOINTS } from '@/utils/constants'; @@ -9,27 +10,35 @@ interface AuthStore { logout: () => void; } -export const useAuthStore = create((set) => ({ - apiKey: null, - isAuthenticated: false, +export const useAuthStore = create()( + persist( + (set, get) => ({ + apiKey: null, + isAuthenticated: false, - login: async (key: string) => { - // Temporarily set the key so the apiGet will use it - set({ apiKey: key }); + login: async (key: string) => { + // Temporarily set the key so the apiGet will use it + set({ apiKey: key }); - try { - // Validate by calling clients endpoint (requires admin scope) - await apiGet(ENDPOINTS.CLIENTS); - set({ isAuthenticated: true }); - return true; - } catch { - // Reset on failure - set({ apiKey: null, isAuthenticated: false }); - return false; + try { + // Validate by calling clients endpoint (requires admin scope) + await apiGet(ENDPOINTS.CLIENTS); + set({ isAuthenticated: true }); + return true; + } catch { + // Reset on failure + set({ apiKey: null, isAuthenticated: false }); + return false; + } + }, + + logout: () => { + set({ apiKey: null, isAuthenticated: false }); + }, + }), + { + name: 'imago-auth', + partialize: (state) => ({ apiKey: state.apiKey, isAuthenticated: state.isAuthenticated }), } - }, - - logout: () => { - set({ apiKey: null, isAuthenticated: false }); - }, -})); + ) +);