fix: persistance du login admin via localStorage (zustand persist)
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { create } from 'zustand';
|
import { create } from 'zustand';
|
||||||
|
import { persist } from 'zustand/middleware';
|
||||||
import { apiGet } from '@/api/client';
|
import { apiGet } from '@/api/client';
|
||||||
import { ENDPOINTS } from '@/utils/constants';
|
import { ENDPOINTS } from '@/utils/constants';
|
||||||
|
|
||||||
@@ -9,27 +10,35 @@ interface AuthStore {
|
|||||||
logout: () => void;
|
logout: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useAuthStore = create<AuthStore>((set) => ({
|
export const useAuthStore = create<AuthStore>()(
|
||||||
apiKey: null,
|
persist(
|
||||||
isAuthenticated: false,
|
(set, get) => ({
|
||||||
|
apiKey: null,
|
||||||
|
isAuthenticated: false,
|
||||||
|
|
||||||
login: async (key: string) => {
|
login: async (key: string) => {
|
||||||
// Temporarily set the key so the apiGet will use it
|
// Temporarily set the key so the apiGet will use it
|
||||||
set({ apiKey: key });
|
set({ apiKey: key });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Validate by calling clients endpoint (requires admin scope)
|
// Validate by calling clients endpoint (requires admin scope)
|
||||||
await apiGet(ENDPOINTS.CLIENTS);
|
await apiGet(ENDPOINTS.CLIENTS);
|
||||||
set({ isAuthenticated: true });
|
set({ isAuthenticated: true });
|
||||||
return true;
|
return true;
|
||||||
} catch {
|
} catch {
|
||||||
// Reset on failure
|
// Reset on failure
|
||||||
set({ apiKey: null, isAuthenticated: false });
|
set({ apiKey: null, isAuthenticated: false });
|
||||||
return 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 });
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|||||||
Reference in New Issue
Block a user