passport
passport copied to clipboard
Using node's promisify with passport
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?
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.
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.
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.
I'm getting:
TypeError: Cannot read property 'query' of undefined at Strategy.OAuth2Strategy.authenticate
When trying to do like this, any idea why?