Skip to content

Commit 77bcb80

Browse files
🛠️ fix: Remove expiresAt field when setting expiry to "never" (danny-avila#4294)
1 parent ee5b96a commit 77bcb80

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

api/server/services/UserService.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ const getUserKeyExpiry = async ({ userId, name }) => {
103103
* @param {string} params.userId - The unique identifier for the user.
104104
* @param {string} params.name - The name associated with the key.
105105
* @param {string} params.value - The value to be encrypted and stored as the key's value.
106-
* @param {Date} params.expiresAt - The expiry date for the key.
106+
* @param {Date} params.expiresAt - The expiry date for the key [optional]
107107
* @returns {Promise<Object>} The updated or newly inserted key document.
108108
* @description This function either updates an existing user key or inserts a new one into the database,
109-
* after encrypting the provided value. It sets the provided expiry date for the key.
109+
* after encrypting the provided value. It sets the provided expiry date for the key (or unsets for no expiry).
110110
*/
111111
const updateUserKey = async ({ userId, name, value, expiresAt = null }) => {
112112
const encryptedValue = await encrypt(value);
@@ -115,13 +115,15 @@ const updateUserKey = async ({ userId, name, value, expiresAt = null }) => {
115115
name,
116116
value: encryptedValue,
117117
};
118-
119-
// Only add expiresAt to the update object if it's not null
118+
const updateQuery = { $set: updateObject };
119+
// add expiresAt to the update object if it's not null
120120
if (expiresAt) {
121121
updateObject.expiresAt = new Date(expiresAt);
122+
} else {
123+
// make sure to remove if already present
124+
updateQuery.$unset = { expiresAt };
122125
}
123-
124-
return await Key.findOneAndUpdate({ userId, name }, updateObject, {
126+
return await Key.findOneAndUpdate({ userId, name }, updateQuery, {
125127
upsert: true,
126128
new: true,
127129
}).lean();

0 commit comments

Comments
 (0)