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

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

Closed
michaeljota opened this issue Jan 30, 2016 · 7 comments
Closed

Comments

@michaeljota
Copy link

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-
Copy link

+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
Copy link
Author

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
Copy link
Author

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

@michaeljota
Copy link
Author

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
Copy link

fxpoet commented May 4, 2016

+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
Copy link

kurdin commented Jun 26, 2016

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

Can you add this please and release new version?

@caseyWebb
Copy link
Contributor

PRs! PRs for everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants