Skip to content

Making passport compatible with graphql #542

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

shalkam
Copy link

@shalkam shalkam commented Jan 26, 2017

Hi,
So, in this commit I've enabled returning a value from the authenticate method of any strategy.
I've tested it with this strategy passport-graphql-mongoose that returns a promise instead of depending on callbacks.
I had to do this because if the authentication method is asynchronous, I always get an empty result and the callback takes effect only later on.
Now I can do something like this:

  async login(root, params, context, ast) {
    let res = {};
    if (typeof context.req.user === 'undefined') {
      context.req.body.username = params.username;
      context.req.body.password = params.password;
      const auth = passport.authenticate('local')(context.req);
      const user = await auth['local']
        .then(user => {
          context.req.logIn(user, function(err) {
            res = user;
            res.message = 'Logged in successfully!';
          });
        })
        .catch(err => {
          res = err;
        });
    } else {
      res = new Error('Already logged in');
    }
    return res;
  }

regards,
Mostafa

Enabled returning a value from the authenticate method of any strategy
@coveralls
Copy link

coveralls commented Jan 26, 2017

Coverage Status

Coverage increased (+0.004%) to 99.398% when pulling 9313e5b on shalkam:master into 8de1c66 on jaredhanson:master.

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

Successfully merging this pull request may close these issues.

2 participants