graffiti-mongoose
graffiti-mongoose copied to clipboard
Async hook support
I would like to use bcrypt in order to encrypt user passwords. Basically bcrypt provides both synchronus and asynchronous methods to achieve this.
Regarding the performance I tried the async method inside a mutation pre hook first:
mutation: {
pre: (next, args, context, info) {
bcrypt.hash(args.password, 8, (err, hash) => {
args.password = hash;
console.log(args.password);
next(args, context, info);
});
}
}
Here console.log(args.password)
prints the hash on the console but my query returns a user object with the raw password instead the hash. Calling next(args, context, info)
inside the callback has no effect!
In #123 I found a working mutation hook example so I changed the code above using bcrypt's sync function.
mutation: {
pre: (next, args, context, info) {
args.password = bcrypt.hashSync(args.password, 8)
next(args, context, info);
}
}
Now the same query returns a user object with the hashed password.
mutation {
updateUser(input:{
id: "a00000000000000000000001"
username: "name"
password: "password"
}){changedUser{username,password}}
}
The user object:
{
"data": {
"updateUser": {
"changedUser": {
"username": "name",
"password": "$2a$08$guy.E3lD7P2Yov3gTSUvnurxK/XN7KgjFjTrlLmXPL6IElbBVLukO"
}
}
}
I can't imagine that this is intended since there could be a great demand for async hooks.
Node v4.5.0 Graffity-Mongoose v5.3.0