From 87ac723fcb017e8138b67d1b88ff1c8211e2b0e4 Mon Sep 17 00:00:00 2001 From: = Date: Thu, 13 Mar 2025 01:45:49 +0530 Subject: [PATCH] feat: resolved renew token not renewing --- .../identity-access-token-service.ts | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/backend/src/services/identity-access-token/identity-access-token-service.ts b/backend/src/services/identity-access-token/identity-access-token-service.ts index 47d1791d2..a51d80e41 100644 --- a/backend/src/services/identity-access-token/identity-access-token-service.ts +++ b/backend/src/services/identity-access-token/identity-access-token-service.ts @@ -78,9 +78,7 @@ export const identityAccessTokenServiceFactory = ({ const renewAccessToken = async ({ accessToken }: TRenewAccessTokenDTO) => { const appCfg = getConfig(); - const decodedToken = jwt.verify(accessToken, appCfg.AUTH_SECRET) as JwtPayload & { - identityAccessTokenId: string; - }; + const decodedToken = jwt.verify(accessToken, appCfg.AUTH_SECRET) as TIdentityAccessTokenJwtPayload; if (decodedToken.authTokenType !== AuthTokenType.IDENTITY_ACCESS_TOKEN) { throw new BadRequestError({ message: "Only identity access tokens can be renewed" }); } @@ -127,7 +125,23 @@ export const identityAccessTokenServiceFactory = ({ accessTokenLastRenewedAt: new Date() }); - return { accessToken, identityAccessToken: updatedIdentityAccessToken }; + const renewedToken = jwt.sign( + { + identityId: decodedToken.identityId, + clientSecretId: decodedToken.clientSecretId, + identityAccessTokenId: decodedToken.identityAccessTokenId, + authTokenType: AuthTokenType.IDENTITY_ACCESS_TOKEN + } as TIdentityAccessTokenJwtPayload, + appCfg.AUTH_SECRET, + // akhilmhdh: for non-expiry tokens you should not even set the value, including undefined. Even for undefined jsonwebtoken throws error + Number(identityAccessToken.accessTokenTTL) === 0 + ? undefined + : { + expiresIn: Number(identityAccessToken.accessTokenTTL) + } + ); + + return { accessToken: renewedToken, identityAccessToken: updatedIdentityAccessToken }; }; const revokeAccessToken = async (accessToken: string) => {