From 5764a33ae8cadff6e48f5e7cb6d31e977e4d8ab9 Mon Sep 17 00:00:00 2001 From: Amadeusz Winogrodzki Date: Sat, 15 Jun 2024 20:28:53 +0200 Subject: [PATCH] fix: store latest valid id token on client --- src/next/client.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/next/client.ts b/src/next/client.ts index cb25658a..81352a16 100644 --- a/src/next/client.ts +++ b/src/next/client.ts @@ -7,12 +7,27 @@ export interface GetValidIdTokenOptions { checkRevoked?: boolean; } +let serverIdTokenCacheMap: Record = {}; + +function getLatestIdToken(serverIdToken: string) { + if (!serverIdTokenCacheMap[serverIdToken]) { + return serverIdToken; + } + + return serverIdTokenCacheMap[serverIdToken]; +} + +function saveLatestIdToken(serverIdToken: string, idToken: string) { + serverIdTokenCacheMap = {[serverIdToken]: idToken}; +} + export async function getValidIdToken({ serverIdToken, refreshTokenUrl, checkRevoked }: GetValidIdTokenOptions): Promise { - const payload = decodeJwt(serverIdToken); + const token = getLatestIdToken(serverIdToken); + const payload = decodeJwt(token); const exp = payload?.exp ?? 0; if (!checkRevoked && exp > Date.now() / 1000) { @@ -28,6 +43,8 @@ export async function getValidIdToken({ ); } + saveLatestIdToken(serverIdToken, response.idToken); + return response.idToken; }