passport icon indicating copy to clipboard operation
passport copied to clipboard

Using node's promisify with passport

Open walleXD opened this issue 7 years ago • 4 comments

Hi,

I am using graphql which expects promises in it's resolvers and so I end up wrapping functions like passport.authenticate with a promise like:

export const authenticate = (req, res) =>
  new Promise((resolve, reject) => {
    passport.authenticate(
      'jwt',
      { session: false },
      (err, user) => {
        if (err) reject(new Error(err))
        else if (!user) reject(new Error('Not authenticated'))
        resolve(user)
      })(req, res)
    })

What would be the best approach to use node's builtin promisify module to do this? Any suggestion?

walleXD avatar Sep 06 '17 18:09 walleXD

Thanks that helped a lot. Earlier I was trying to use bluebird to promisify "passport.authenticate" function, but I was not able to pass "(req, res)" to it.

jyotendra avatar Apr 10 '18 06:04 jyotendra

Hi,

I am using graphql which expects promises in it's resolvers and so I end up wrapping functions like passport.authenticate with a promise like:

export const authenticate = (req, res) =>
  new Promise((resolve, reject) => {
    passport.authenticate(
      'jwt',
      { session: false },
      (err, user) => {
        if (err) reject(new Error(err))
        else if (!user) reject(new Error('Not authenticated'))
        resolve(user)
      })(req, res)
    })

What would be the best approach to use node's builtin promisify module to do this? Any suggestion?

Thank you for this code snippet. I was struggling with how to promisify passport.authenticate so I could use it outside of a route related function.

ashishchandr70 avatar Jan 10 '19 17:01 ashishchandr70

Hi guys, is there any way to configure the passport completely asynchronous? I was trying to create the a class like this:

import { OAuth2Strategy } from 'passport-google-oauth';
import passport from 'passport';

class Passport {
  constructor() {
    this.passport = passport;

    this.middlewares();
  }

  middlewares() {
    this.passport.serializeUser((user, done) => {
      done(null, user);
    });
    this.passport.deserializeUser((user, done) => {
      done(null, user);
    });
    this.passport.use('google-auth', this.configGoogleStrategy());
  }

  configGoogleStrategy() {
    const googleStrategyConfig = {
      clientID: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      callbackURL: process.env.GOOGLE_CALLBACK_URL,
    };

    return new OAuth2Strategy(
      googleStrategyConfig,
      (token, refreshToken, profile, done) => {
        return done(null, {
          profile,
          token,
        });
      }
    );
  }
}

export default new Passport().passport;

the configGoogleStrategy() method has no async modifier on function's definition. When I try to convert this method using the promisify from util library, the remaining places where I need to use the property passport returns a non defined function.

Another question that I have is: why some times I can use the promisify like this:

const decoded = await promisify(jwt.verify)(token, authConfig.secret);

and other times I need to call the then() statement?

Any doubts about my question please let me know!

Regards.

camilodsilva avatar Nov 12 '19 16:11 camilodsilva

I'm getting: TypeError: Cannot read property 'query' of undefined at Strategy.OAuth2Strategy.authenticate

When trying to do like this, any idea why?

minifisk avatar Feb 03 '21 17:02 minifisk