From 0346534831a0c653ae6cbbe46187d74babce9bf3 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Thu, 4 Apr 2024 17:06:28 +0530 Subject: [PATCH] refactor: suggested review changes Signed-off-by: Pranav C --- packages/nocodb/src/models/UserRefreshToken.ts | 9 +++++++-- packages/nocodb/src/services/users/users.service.ts | 7 ++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/nocodb/src/models/UserRefreshToken.ts b/packages/nocodb/src/models/UserRefreshToken.ts index a349e71c15..ae4d805fd0 100644 --- a/packages/nocodb/src/models/UserRefreshToken.ts +++ b/packages/nocodb/src/models/UserRefreshToken.ts @@ -6,7 +6,12 @@ import { MetaTable } from '~/utils/globals'; import { parseMetaProp, stringifyMetaProp } from '~/utils/modelUtils'; const NC_REFRESH_TOKEN_EXP_IN_DAYS = - +process.env.NC_REFRESH_TOKEN_EXP_IN_DAYS || 90; + parseInt(process.env.NC_REFRESH_TOKEN_EXP_IN_DAYS, 10) || 90; + +// throw error if user provided invalid value +if (NC_REFRESH_TOKEN_EXP_IN_DAYS <= 0) { + throw new Error('NC_REFRESH_TOKEN_EXP_IN_DAYS must be a positive number'); +} export default class UserRefreshToken { fk_user_id: string; @@ -43,7 +48,7 @@ export default class UserRefreshToken { 'meta', ]); - // set default expiry as 90 days if missing + // set expiry based on the env or default value if (!('expires_at' in insertObj)) { insertObj.expires_at = dayjs() .add(NC_REFRESH_TOKEN_EXP_IN_DAYS, 'day') diff --git a/packages/nocodb/src/services/users/users.service.ts b/packages/nocodb/src/services/users/users.service.ts index 4ccfbf14f9..9010192250 100644 --- a/packages/nocodb/src/services/users/users.service.ts +++ b/packages/nocodb/src/services/users/users.service.ts @@ -380,7 +380,12 @@ export class UsersService { const refreshToken = randomTokenString(); - await UserRefreshToken.updateOldToken(oldRefreshToken, refreshToken); + try { + await UserRefreshToken.updateOldToken(oldRefreshToken, refreshToken); + } catch (error) { + console.error('Failed to update old refresh token:', error); + NcError.internalServerError('Failed to update refresh token'); + } setTokenCookie(param.res, refreshToken);