Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ui/src/data-services/hooks/auth/tests/useUserInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('useUserInfo', () => {
axios.get.mockImplementation(() =>
Promise.resolve({ data: { id: 1, email: 'user@insectai.org' } })
)
localStorage.setItem(AUTH_TOKEN_STORAGE_KEY, 'example-token') // Simulate logged in user

// Run
const { result } = renderHook(() => useUserInfo(), { wrapper: AppMock })
Expand All @@ -39,7 +40,7 @@ describe('useUserInfo', () => {

// Run
const { result } = renderHook(() => useUserInfo(), { wrapper: AppMock })
await waitFor(() => expect(result.current.error).not.toBeNull())
await waitFor(() => expect(result.current.userInfo).toBeUndefined())

// Check
expect(localStorage.getItem(AUTH_TOKEN_STORAGE_KEY)).toBeNull()
Expand Down
4 changes: 3 additions & 1 deletion ui/src/data-services/hooks/auth/useAuthorizedQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { getAuthHeader } from 'data-services/utils'
import { useUser } from 'utils/user/userContext'

export const useAuthorizedQuery = <T>({
enabled,
onError,
queryKey = [],
refetchInterval,
retry,
staleTime,
url,
}: {
enabled?: boolean
onError?: (error: unknown) => void
queryKey?: QueryKey
refetchInterval?: number
Expand All @@ -19,8 +21,8 @@ export const useAuthorizedQuery = <T>({
url: string
}) => {
const { user } = useUser()

const { data, isLoading, isFetching, isSuccess, error } = useQuery({
enabled,
onError,
queryKey,
queryFn: () =>
Expand Down
1 change: 1 addition & 0 deletions ui/src/data-services/hooks/auth/useUserInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const REFETCH_INTERVAL = 10000 // Refetch every 10 second
export const useUserInfo = () => {
const { user, clearToken } = useUser()
const { data, isLoading, error } = useAuthorizedQuery<ServerUserInfo>({
enabled: user.loggedIn,
queryKey: [API_ROUTES.ME],
url: `${API_URL}/${API_ROUTES.ME}/`,
refetchInterval: REFETCH_INTERVAL,
Expand Down