-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Fixed Expired JWT Token By Refreshing it (#1353)
* enable refresh token for Reactions * make refreshIdToken reusable (hook) * use refreshed id token for all apis * fix order * log in again in case the refresh token is expired * move auth0_scope to .env
- Loading branch information
1 parent
aefdeb8
commit fbbbaa3
Showing
11 changed files
with
88 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useAuth0 } from '@auth0/auth0-react'; | ||
|
||
export const useAuthService = () => { | ||
const { getIdTokenClaims, getAccessTokenSilently, loginWithRedirect } = | ||
useAuth0(); | ||
|
||
const fetchIdToken = async () => { | ||
try { | ||
const claims = await getIdTokenClaims(); | ||
const expiryTime = claims.exp * 1000; | ||
const currentTime = new Date().getTime(); | ||
|
||
if (expiryTime - currentTime < 60000) { | ||
await getAccessTokenSilently({ | ||
audience: process.env.AUTH0_DOMAIN, | ||
scope: process.env.AUTH0_SCOPE, | ||
cacheMode: 'off', | ||
}); | ||
|
||
const refreshedClaims = await getIdTokenClaims(); | ||
return refreshedClaims.__raw; | ||
} | ||
return claims.__raw; | ||
} catch (error) { | ||
if (window.confirm('Your session has expired. Please log in again')) { | ||
const currentPage = | ||
typeof window !== 'undefined' | ||
? window.location.pathname.split('/').pop() | ||
: null; | ||
await loginWithRedirect({ | ||
appState: { | ||
targetUrl: currentPage, | ||
}, | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
return { fetchIdToken }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters