32 lines
924 B
TypeScript
32 lines
924 B
TypeScript
import { useEffect } from 'react';
|
|
import { useAuthStore } from '@/store/authStore';
|
|
import { useCurrentUser } from '@/api/hooks';
|
|
import { useUserPermissions } from '@/hooks/useUserPermissions';
|
|
|
|
export function useAuth() {
|
|
const store = useAuthStore();
|
|
const { data, isLoading, isError, error } = useCurrentUser();
|
|
|
|
// Load permissions after authentication
|
|
useUserPermissions();
|
|
|
|
useEffect(() => {
|
|
if (isError) {
|
|
const status = (error as any)?.status || 0;
|
|
// Only logout on 401 Unauthorized — other errors are transient
|
|
if (status === 401) {
|
|
store.setAuthenticated(false);
|
|
store.setUser(null);
|
|
}
|
|
// For 403, 500, network errors: keep session, don't logout
|
|
}
|
|
}, [isError, error, store]);
|
|
|
|
return {
|
|
user: store.user,
|
|
isAuthenticated: store.isAuthenticated,
|
|
isLoading: isLoading && !store.user,
|
|
currentTenant: store.currentTenant,
|
|
};
|
|
}
|