Skip to content

Commit

Permalink
Merge pull request #142 from jbrunton/fix-require-auth
Browse files Browse the repository at this point in the history
fix: useAccessToken hook
  • Loading branch information
jbrunton authored Aug 17, 2024
2 parents f9b3515 + 3785519 commit 77ea8ed
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 77ea8ed

Please sign in to comment.