Files
rack-planner/client/src/store/useAuthStore.ts
T

38 lines
864 B
TypeScript
Raw Normal View History

import { create } from 'zustand';
import { apiClient } from '../api/client';
interface AuthState {
isAuthenticated: boolean;
loading: boolean;
checkAuth: () => Promise<void>;
login: (username: string, password: string) => Promise<void>;
logout: () => Promise<void>;
}
export const useAuthStore = create<AuthState>((set) => ({
isAuthenticated: false,
loading: true,
checkAuth: async () => {
try {
await apiClient.auth.me();
set({ isAuthenticated: true, loading: false });
} catch {
set({ isAuthenticated: false, loading: false });
}
},
login: async (username, password) => {
await apiClient.auth.login(username, password);
set({ isAuthenticated: true });
},
logout: async () => {
try {
await apiClient.auth.logout();
} finally {
set({ isAuthenticated: false });
}
},
}));