scottwrobinson / camo

A class-based ES6 ODM for Mongo-like databases.
557 stars 81 forks source link

Neither 'PostValidate' or 'PreSave' hooks are not altering the document. #43

Closed michaeljota closed 8 years ago

michaeljota commented 8 years ago

I don't know if i'm doing this properly, but I'm trying to hash the password in either 'PreSave' or 'PostValidate' (I supposse PostValidate get's calling just before PreSave).

PostValidate got called, I think PreSave is not. But in either case, I doesn't make permanent changes to the document. The changes that are made, are lost when they get save.

    postValidate () {
        if(this.password) {
            console.log('Salting');
            this.salt = new Buffer (crypto.randomBytes(16).toString('base64'), 'base64');
            console.log(this.salt);
            this.password = this.hashPassword(this.password);
        }
        this.updated = Date.now();
    }

    postSave () {
        console.log('Post save');
        console.log(this);
    }

The console outputs show they are being called. PostSave show changes in the document, but it the datastorage (I'm using NeDB) there is not salt in the document, and the password it's not hashed.

Any thoughs?

Thanks for camo. I don't think I ever got to post so much in a issue list, that is not mine own.

PS: I'm trying to using this with Passport, but all the examples are with 'mongoose'. I'm trying to write all the examples to camo, but I've fail, so far. Have you use Camo with Passport? How did you?

vfreitas- commented 8 years ago

+1 Same here, I'm trying to set a property value in the preSave hook, but the changes are being lost.

preSave() {
    this.slug = slugify(this.title); //this.title = 'some phrase'
    console.log(this.slug); // 'some-phrase'
        //mongodb = null
}
michaeljota commented 8 years ago

He is unable to work on it right now. But as soon as he can, he will look at it.

I just created a function to hash the password and called before the saving. It might work (It should at least, I hadn't try it).

It might work for you as well. :D

michaeljota commented 8 years ago

Update: It works!. Just use the function that make changes in the object and called before salving. :D.

michaeljota commented 8 years ago

Update2: I use password getter and setter to hash the password.

    set password (password) {
        this._password = password;
        this.salt = this.makeSalt();
        this.hashedPassword = this.encryptPassword(password);
    }

    get password () {
        return this._password;
    }

That solve my problem too, and it's easies to use, 'cause I just create the document with the 'password' param, and it actually hash the password, resulting:

{"name":"Test User","email":"test@test.com","hashedPassword":"(hashedPassword)","role":"user","provider":"local","salt":"(salt)","_id":"KNW4kT5nqj5cTg4V"}
fxpoet commented 8 years ago

+1 same issue.but I solved with stopgap. add single line code at line 138, node_modules/camo/lib/document.js

data = that._toData({_id:false}); line139: return DB().save(that.collectionName(), that._id, data);

kurdin commented 8 years ago

data = that._toData({_id:false});

Can you add this please and release new version?

caseyWebb commented 8 years ago

PRs! PRs for everyone!