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

Async hook support #164

Open
kruschid opened this issue Sep 28, 2016 · 2 comments
Open

Async hook support #164

kruschid opened this issue Sep 28, 2016 · 2 comments

Comments

@kruschid
Copy link

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

@pie6k
Copy link

pie6k commented Oct 3, 2016

Same issue

@tothandras
Copy link
Contributor

I'll take a look!

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

3 participants