From 971a6faaf6008e91c70d011502827eb782e4ed05 Mon Sep 17 00:00:00 2001 From: Swarnendu Bhandari <94161758+Swarnendu0123@users.noreply.github.com> Date: Tue, 13 Feb 2024 16:35:27 +0530 Subject: [PATCH 1/2] Asynchronous Handling Issue in API Key Hashing Middleware fixed --- server/models/user.js | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/server/models/user.js b/server/models/user.js index 68b9750847..026aa5f565 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -115,37 +115,28 @@ userSchema.pre('save', function checkPassword(next) { /** * API keys hash middleware */ -userSchema.pre('save', function checkApiKey(next) { - // eslint-disable-line consistent-return +userSchema.pre('save', async function checkApiKey(next) { const user = this; if (!user.isModified('apiKeys')) { next(); return; } - let hasNew = false; - user.apiKeys.forEach((k) => { - if (k.isNew) { - hasNew = true; - bcrypt.genSalt(10, (err, salt) => { - // eslint-disable-line consistent-return - if (err) { - next(err); - return; - } - bcrypt.hash(k.hashedKey, salt, (innerErr, hash) => { - if (innerErr) { - next(innerErr); - return; - } - k.hashedKey = hash; - next(); - }); - }); - } - }); - if (!hasNew) next(); -}); + try { + await Promise.all(user.apiKeys.map(async (k) => { + if (k.isNew) { + const salt = await bcrypt.genSalt(10); + const hash = await bcrypt.hash(k.hashedKey, salt); + k.hashedKey = hash; + k.isNew = false; // Set isNew flag to false after hashing + } + })); + + next(); // Call next if all operations are successful + } catch (err) { + next(err); // Pass any error to the next middleware + } +}); userSchema.virtual('id').get(function idToString() { return this._id.toHexString(); }); From fd15b28b2192f3d6b9b4b3a0f4cdf245dfa0c275 Mon Sep 17 00:00:00 2001 From: Swarnendu Bhandari <94161758+Swarnendu0123@users.noreply.github.com> Date: Tue, 13 Feb 2024 23:27:47 +0530 Subject: [PATCH 2/2] Updated --- server/models/user.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/server/models/user.js b/server/models/user.js index 026aa5f565..f458085b31 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -118,25 +118,28 @@ userSchema.pre('save', function checkPassword(next) { userSchema.pre('save', async function checkApiKey(next) { const user = this; if (!user.isModified('apiKeys')) { - next(); - return; + next(); + return; } try { - await Promise.all(user.apiKeys.map(async (k) => { - if (k.isNew) { - const salt = await bcrypt.genSalt(10); - const hash = await bcrypt.hash(k.hashedKey, salt); - k.hashedKey = hash; - k.isNew = false; // Set isNew flag to false after hashing - } - })); + const hashTasks = user.apiKeys + .filter(k => k.isNew) + .map(async (k) => { + const salt = await bcrypt.genSalt(10); + const hash = await bcrypt.hash(k.hashedKey, salt); + k.hashedKey = hash; + }); + + await Promise.all(hashTasks); - next(); // Call next if all operations are successful + next(); } catch (err) { - next(err); // Pass any error to the next middleware + next(err); } }); + + userSchema.virtual('id').get(function idToString() { return this._id.toHexString(); });