import { useState, useEffect } from 'react'; import { useApiRequest } from './useApi'; // User interfaces export interface User { id: string; username: string; email: string; fullName: string; language: string; enabled: boolean; privilege: string; authenticationAuthority: string; mandateId: string; } export type UserUpdateData = Partial>; // Current user hook export function useCurrentUser() { const [user, setUser] = useState(null); const { request, isLoading, error } = useApiRequest(); const fetchCurrentUser = async () => { try { const data = await request({ url: '/api/local/me', method: 'get' }); setUser(data); } catch (error) { setUser(null); } }; const logout = async (msalInstance?: any) => { if (!user) { throw new Error('No user to logout'); } try { let logoutEndpoint = '/api/local/logout'; // Determine the correct logout endpoint based on authentication authority if (user.authenticationAuthority === 'msft') { logoutEndpoint = '/api/msft/logout'; } else if (user.authenticationAuthority === 'local') { logoutEndpoint = '/api/local/logout'; } await request({ url: logoutEndpoint, method: 'post' }); // Clear user state after successful logout setUser(null); // Clear any local storage data localStorage.clear(); // Handle MSAL logout for Microsoft authentication if (user.authenticationAuthority === 'msft' && msalInstance) { try { await msalInstance.logoutRedirect({ onRedirectNavigate: () => true }); return; // MSAL will handle the redirect } catch (msalError) { console.error('MSAL logout failed:', msalError); // Continue with regular redirect if MSAL logout fails } } // Redirect to login or home page window.location.href = '/login'; } catch (error) { console.error('Logout failed:', error); throw error; } }; useEffect(() => { fetchCurrentUser(); }, []); return { user, error, isLoading, refetch: fetchCurrentUser, logout }; } // Organization users hook (list, update, delete) export function useOrgUsers() { const [users, setUsers] = useState([]); const { request, isLoading: loading, error } = useApiRequest(); const fetchUsers = async () => { try { const data = await request({ url: '/api/users/', method: 'get' }); setUsers(data); } catch (error) { // Error is already handled by useApiRequest } }; const updateUser = async (userId: string, userData: User) => { await request({ url: `/api/users/${userId}`, method: 'put', data: userData }); await fetchUsers(); // Refresh the list after update }; const deleteUser = async (userId: string) => { await request({ url: `/api/users/${userId}`, method: 'delete' }); await fetchUsers(); // Refresh the list after deletion }; const getUser = async (userId: string): Promise => { return await request({ url: `/api/users/${userId}`, method: 'get' }); }; useEffect(() => { fetchUsers(); }, []); return { users, loading, error, refetch: fetchUsers, updateUser, deleteUser, getUser }; } // Individual user operations hook (for use when you don't need the full list) export function useUser() { const { request, isLoading, error } = useApiRequest(); const getUser = async (userId: string): Promise => { return await request({ url: `/api/users/${userId}`, method: 'get' }); }; const updateUser = async (userId: string, userData: User): Promise => { return await request({ url: `/api/users/${userId}`, method: 'put', data: userData }); }; const deleteUser = async (userId: string): Promise => { await request({ url: `/api/users/${userId}`, method: 'delete' }); }; return { getUser, updateUser, deleteUser, isLoading, error }; }