fix: sync backend and frontend token refresh logic (#614)

* sync backend and frontend token refresh logic

* return react strict mode

---------

Co-authored-by: Igor Loskutov <igor.loskutoff@gmail.com>
This commit is contained in:
Igor Monadical
2025-09-08 10:40:18 -04:00
committed by GitHub
parent 02a3938822
commit 5a5b323382
3 changed files with 15 additions and 8 deletions

View File

@@ -10,6 +10,7 @@ import {
import {
REFRESH_ACCESS_TOKEN_BEFORE,
REFRESH_ACCESS_TOKEN_ERROR,
shouldRefreshToken,
} from "./auth";
import {
getTokenCache,
@@ -85,9 +86,13 @@ export const authOptions: AuthOptions = {
"currentToken from cache",
JSON.stringify(currentToken, null, 2),
"will be returned?",
currentToken && Date.now() < currentToken.token.accessTokenExpires,
currentToken &&
!shouldRefreshToken(currentToken.token.accessTokenExpires),
);
if (currentToken && Date.now() < currentToken.token.accessTokenExpires) {
if (
currentToken &&
!shouldRefreshToken(currentToken.token.accessTokenExpires)
) {
return currentToken.token;
}
@@ -128,7 +133,7 @@ async function lockedRefreshAccessToken(
if (cached) {
if (Date.now() - cached.timestamp > TOKEN_CACHE_TTL) {
await deleteTokenCache(tokenCacheRedis, `token:${token.sub}`);
} else if (Date.now() < cached.token.accessTokenExpires) {
} else if (!shouldRefreshToken(cached.token.accessTokenExpires)) {
console.debug("returning cached token", cached.token);
return cached.token;
}