1

I have the problem that the salt generation and password encoding is are ready when the document gets already persisted. => mongoosejs doesn't wait until the hashing is finished, so we have no passwords and salts in database (but I can console.log them :).

Is there something to avoid this behaviour?

  1. A waiter function?
  2. Or should I move the encoding part into some pre-save middleware?

encoding method

UserSchema.methods.encodePassword = function(password) {
    crypto.randomBytes(32, function(err, buf) {
        this.salt = buf.toString('hex');
        console.log(this.salt);
        crypto.pbkdf2(password, this.salt, 25000, 512, function(err, encodedPassword) {
            if (err) throw err;
            this.password = encodedPassword;
            console.log(this.password);
        }.bind(this));
    });
};

route action

// creates a new user
app.post('/users', function(req, res) {
    // create new user
    var user = new User();
    // assign post
    user.username = req.body.username;
    user.email = req.body.email;
    user.encodePassword(req.body.password);
    user.save(function(err, user) {
        console.log(user);
        if (!err) return res.send(err, 500);
        return res.json(user);
    });
});

1 Answer 1

1

You'll want encodePassword to take a callback so you can call it when you're done.

UserSchema.methods.encodePassword = function(password, callback) {
    crypto.randomBytes(32, function(err, buf) {
        this.salt = buf.toString('hex');
        console.log(this.salt);
        crypto.pbkdf2(password, this.salt, 25000, 512, function(err, encodedPassword) {
            if (err) return callback(err);
            this.password = encodedPassword;
            console.log(this.password);
            callback();
        }.bind(this));
    });
};

Now you can wait for it to be done in your route.

app.post('/users', function(req, res) {
    // create new user
    var user = new User();
    // assign post
    user.username = req.body.username;
    user.email = req.body.email;
    user.encodePassword(req.body.password, function(err) {
        if(err) { /* handle error */ }
        user.save(function(err, user) {
            console.log(user);
            if (!err) return res.send(err, 500);
            return res.json(user);
        });
    });
});
2
  • Hey, for some reason this still don't works. I assume that UserSchema.methods.encodePassword doesn't has semantical access to the model. I will check this out
    – bodokaiser
    Jul 19, 2012 at 7:38
  • I will ask at the mongoose google groups. The docs are way to minimalistic. I never know when and how I can access model properties through setters, methods, plugins, however
    – bodokaiser
    Jul 19, 2012 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.