From 1edbfdbce2c900b852ced03095f483ab0e7b5472 Mon Sep 17 00:00:00 2001 From: Marco Beretta <81851188+berry-13@users.noreply.github.com> Date: Fri, 5 Jul 2024 17:15:09 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=91=20feat:=20infinite=20key=20expiry?= =?UTF-8?q?=20(#3252)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/server/services/UserService.js | 29 +++++++++++-------- .../Input/SetKeyDialog/SetKeyDialog.tsx | 21 +++++++++----- client/src/hooks/Input/useUserKey.ts | 9 +++--- client/src/localization/languages/Eng.ts | 1 + 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/api/server/services/UserService.js b/api/server/services/UserService.js index 6c736e4366..c8f8e966ce 100644 --- a/api/server/services/UserService.js +++ b/api/server/services/UserService.js @@ -93,7 +93,7 @@ const getUserKeyExpiry = async ({ userId, name }) => { if (!keyValue) { return { expiresAt: null }; } - return { expiresAt: keyValue.expiresAt }; + return { expiresAt: keyValue.expiresAt || 'never' }; }; /** @@ -108,18 +108,23 @@ const getUserKeyExpiry = async ({ userId, name }) => { * @description This function either updates an existing user key or inserts a new one into the database, * after encrypting the provided value. It sets the provided expiry date for the key. */ -const updateUserKey = async ({ userId, name, value, expiresAt }) => { +const updateUserKey = async ({ userId, name, value, expiresAt = null }) => { const encryptedValue = encrypt(value); - return await Key.findOneAndUpdate( - { userId, name }, - { - userId, - name, - value: encryptedValue, - expiresAt: new Date(expiresAt), - }, - { upsert: true, new: true }, - ).lean(); + let updateObject = { + userId, + name, + value: encryptedValue, + }; + + // Only add expiresAt to the update object if it's not null + if (expiresAt) { + updateObject.expiresAt = new Date(expiresAt); + } + + return await Key.findOneAndUpdate({ userId, name }, updateObject, { + upsert: true, + new: true, + }).lean(); }; /** diff --git a/client/src/components/Input/SetKeyDialog/SetKeyDialog.tsx b/client/src/components/Input/SetKeyDialog/SetKeyDialog.tsx index 0b748a02b7..cb5b0fd469 100644 --- a/client/src/components/Input/SetKeyDialog/SetKeyDialog.tsx +++ b/client/src/components/Input/SetKeyDialog/SetKeyDialog.tsx @@ -41,6 +41,7 @@ const EXPIRY = { ONE_DAY: { display: 'in 1 day', value: 24 * 60 * 60 * 1000 }, ONE_WEEK: { display: 'in 7 days', value: 7 * 24 * 60 * 60 * 1000 }, ONE_MONTH: { display: 'in 30 days', value: 30 * 24 * 60 * 60 * 1000 }, + NEVER: { display: 'never', value: 0 }, }; const SetKeyDialog = ({ @@ -84,7 +85,13 @@ const SetKeyDialog = ({ const submit = () => { const selectedOption = expirationOptions.find((option) => option.display === expiresAtLabel); - const expiresAt = Date.now() + (selectedOption ? selectedOption.value : 0); + let expiresAt; + + if (selectedOption?.value === 0) { + expiresAt = null; + } else { + expiresAt = Date.now() + (selectedOption ? selectedOption.value : 0); + } const saveKey = (key: string) => { saveUserKey(key, expiresAt); @@ -160,12 +167,12 @@ const SetKeyDialog = ({ main={