Skip to content

Asynchronous Setters/Getters #517

@NuckChorris

Description

@NuckChorris

This is important for anyone using bcrypt, including mongoose-auth, which currently relies on synchronous bcrypt functions, which block the entire server while they work.

schema.virtual('password').get( function () {
    return this._password;
}).set( function (password) {
    this._password = password;
    var salt = this.salt = bcrypt.gen_salt_sync(10);
    this.hash = bcrypt.encrypt_sync(password, salt);
});

versus the asynchronous form

schema.virtual('password').get( function () {
    return this._password;
}).set( function (password, done) {
    var _this = this;
    bcrypt.gen_salt(10, function(err, salt) {
        bcrypt.encrypt(password, salt, function(err, hash) {
            _this._password = password;
            _this.salt = salt;
            done();
        });
    });
});

(speaking of which, that is an awful authentication module, at least in the password-auth department — I know I wouldn't trust it.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions