Skip to content

Commit

Permalink
fix: useAccessToken hook
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrunton committed Aug 17, 2024
1 parent f9b3515 commit 3785519
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions client/src/features/auth/hooks/useAccessToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>()
const [isLoading, setIsLoading] = useState(true)

useEffect(() => {
if (isLoadingAuth) {
return
}

if (isAuthenticated) {
const getAccessToken = async () => {
const accessToken = await getAccessTokenSilently({
Expand All @@ -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 }
}

0 comments on commit 3785519

Please sign in to comment.