Skip to content
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

save() concurrency issue when using options.limitAttempts #58

Open
anoirclere opened this issue Oct 7, 2014 · 1 comment
Open

save() concurrency issue when using options.limitAttempts #58

anoirclere opened this issue Oct 7, 2014 · 1 comment
Labels

Comments

@anoirclere
Copy link

Hello,
While using your plugin and implementing a "delete my account" feature, I can ran into a Mongoose error "VersionError: No matching document found".

When a user wants to delete his account, my app opens a modal and asks for his password. If the password is matching (using passport-local-mongoose 'authenticate'), then I set "status: 'deleted'" on the User document and save it. I'm using options.limitAttempts, so passport-local-mongoose performs a first save beforehand to update lastLoginField and attemptsField, but does not use a callback.
Therefore, If "my" second save comes too quickly after "yours", I get this mongoose version conflict.

Could you nest the callback of authenticate inside the call to self.save() ?

~line 105:

        if (hash === self.get(options.hashField)) {
            if (options.limitAttempts){
              self.set(options.lastLoginField, Date.now());
              self.set(options.attemptsField, 0);
              self.save();
            }
            return cb(null, self);
        } else {
           …
        }

=>

        if (hash === self.get(options.hashField)) {
            if (options.limitAttempts){
              self.set(options.lastLoginField, Date.now());
              self.set(options.attemptsField, 0);
              self.save(cb);
            } else {
              return cb(null, self);
            }
        } else {
           …
        }
@saintedlama saintedlama added the bug label Sep 1, 2017
@mvoorberg
Copy link
Contributor

Ran into this same issue while trying to update the user record on successful login - the two saves overlap each other and I was getting duplicate entries (with the same ObjectId) in a subdocument. The code needs to reflect the async nature of .save() and only hit the callback when it returns.

saintedlama added a commit that referenced this issue Oct 25, 2017
Issue #79 and Bug #58, handle save() asynchronously
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants