1
0
mirror of https://github.com/stonith404/pingvin-share.git synced 2024-11-05 07:20:13 +01:00

fix: reduce refresh access token calls

This commit is contained in:
Elias Schneider 2024-02-27 09:40:52 +01:00
parent 4dae7e250a
commit 1aa3d8e5e8
No known key found for this signature in database
GPG Key ID: 07E623B294202B6C
2 changed files with 9 additions and 2 deletions

View File

@ -50,7 +50,12 @@ function App({ Component, pageProps }: AppProps) {
}, [router.pathname]);
useEffect(() => {
setInterval(async () => await authService.refreshAccessToken(), 30 * 1000);
const interval = setInterval(
async () => await authService.refreshAccessToken(),
2 * 60 * 1000, // 2 minutes
);
return () => clearInterval(interval);
}, []);
useEffect(() => {

View File

@ -36,8 +36,10 @@ const signOut = async () => {
const refreshAccessToken = async () => {
try {
const accessToken = getCookie("access_token") as string;
// If the access token expires in less than 2 minutes refresh it
if (
!accessToken ||
accessToken &&
(jose.decodeJwt(accessToken).exp ?? 0) * 1000 < Date.now() + 2 * 60 * 1000
) {
await api.post("/auth/token");