From 3785519291138360aaee7b630281b9c2443e12ff Mon Sep 17 00:00:00 2001 From: John Brunton Date: Sat, 17 Aug 2024 22:12:21 +0100 Subject: [PATCH] fix: useAccessToken hook --- client/src/features/auth/hooks/useAccessToken.tsx | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/client/src/features/auth/hooks/useAccessToken.tsx b/client/src/features/auth/hooks/useAccessToken.tsx index 57c20755..116455f9 100644 --- a/client/src/features/auth/hooks/useAccessToken.tsx +++ b/client/src/features/auth/hooks/useAccessToken.tsx @@ -2,11 +2,15 @@ import { useAuth0 } from '@auth0/auth0-react' import { useEffect, useState } from 'react' export const useAccessToken = (): { accessToken: string | undefined; isLoading: boolean } => { - const { isAuthenticated, getAccessTokenSilently } = useAuth0() + const { isAuthenticated, getAccessTokenSilently, isLoading: isLoadingAuth } = useAuth0() const [accessToken, setAccessToken] = useState() const [isLoading, setIsLoading] = useState(true) useEffect(() => { + if (isLoadingAuth) { + return + } + if (isAuthenticated) { const getAccessToken = async () => { const accessToken = await getAccessTokenSilently({ @@ -17,8 +21,10 @@ export const useAccessToken = (): { accessToken: string | undefined; isLoading: setIsLoading(false) } getAccessToken() + } else { + setIsLoading(false) } - }, [isAuthenticated, isLoading]) + }, [isAuthenticated, isLoading, isLoadingAuth]) return { accessToken, isLoading } }