Skip to content

Asynchronous Handling Issue in API Key Hashing Middleware #3338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 21, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,35 +115,46 @@
/**
* API keys hash middleware
*/
userSchema.pre('save', function checkApiKey(next) {

Check failure on line 118 in server/models/user.js

View workflow job for this annotation

GitHub Actions / Test and lint code base

Expected to return a value at the end of function 'checkApiKey'
// eslint-disable-line consistent-return
const user = this;
if (!user.isModified('apiKeys')) {
next();
return;
return next();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is a lot neater!

}

let hasNew = false;
let pendingTasks = 0;
let nextCalled = false;

const done = (err) => {
if (nextCalled) return;
if (err) {
nextCalled = true;
return next(new Error(err)); // ✅ Pass Error object

Check failure on line 132 in server/models/user.js

View workflow job for this annotation

GitHub Actions / Test and lint code base

Arrow function expected no return value
}
if (--pendingTasks === 0) {

Check failure on line 134 in server/models/user.js

View workflow job for this annotation

GitHub Actions / Test and lint code base

Unary operator '--' used
nextCalled = true;
return next();

Check failure on line 136 in server/models/user.js

View workflow job for this annotation

GitHub Actions / Test and lint code base

Arrow function expected no return value
}
};

user.apiKeys.forEach((k) => {
if (k.isNew) {
hasNew = true;
pendingTasks++;

Check failure on line 143 in server/models/user.js

View workflow job for this annotation

GitHub Actions / Test and lint code base

Unary operator '++' used
bcrypt.genSalt(10, (err, salt) => {

Check failure on line 144 in server/models/user.js

View workflow job for this annotation

GitHub Actions / Test and lint code base

Expected to return a value at the end of arrow function
// eslint-disable-line consistent-return
if (err) {
next(err);
return;
}
if (err) return done(err);
bcrypt.hash(k.hashedKey, salt, (innerErr, hash) => {

Check failure on line 146 in server/models/user.js

View workflow job for this annotation

GitHub Actions / Test and lint code base

Expected to return a value at the end of arrow function
if (innerErr) {
next(innerErr);
return;
}
if (innerErr) return done(innerErr);
k.hashedKey = hash;
next();
done();
});
});
}
});
if (!hasNew) next();

if (!hasNew) {
return next();
}
});

userSchema.virtual('id').get(function idToString() {
Expand Down
Loading