Passport login and persisting session

Is the user now in a session on my server? No, You need to use the express-session middleware before app.use(passport.session()); to actually store the session in memory/database. This middleware is responsible for setting cookies to browsers and converts the cookies sent by browsers into req.session object. PassportJS only uses that object to further deserialize the … Read more

Use multiple local strategies in PassportJS

You can name your local strategies to separate them. // use two LocalStrategies, registered under user and sponsor names // add other strategies for more authentication flexibility passport.use(‘user-local’, new LocalStrategy({ usernameField: ’email’, passwordField: ‘password’ // this is the virtual field on the model }, function(email, password, done) { User.findOne({ email: email }, function(err, user) { … Read more

JavaScript wait for asynchronous function in if statement [duplicate]

I do this exact thing using async/await in my games code here Assuming req.isLoggedIn() returns a boolean, it’s as simple as: const isLoggedIn = await req.isLoggedIn(); if (isLoggedIn) { // do login stuff } Or shorthand it to: if (await req.isLoggedIn()) { // do stuff } Make sure you have that inside an async function … Read more

Passport-Facebook authentication is not providing email for all Facebook accounts

Make sure these two things are in your code: passport.use(new FacebookStrategy({ clientID: ‘CLIENT_ID’, clientSecret: ‘CLIENT_SECRET’, callbackURL: “http://www.example.com/auth/facebook/callback” passReqToCallback : true, profileFields: [‘id’, ’emails’, ‘name’] //This }, and this: app.get(‘/connect/facebook’, passport.authorize(‘facebook’, { scope : [’email’] })); This gives you access to the following: profile.id profile.name.givenName profile.name.familyName profile.emails The last one being an array, so use profile.emails[0].value … Read more

passport’s req.isAuthenticated always returning false, even when I hardcode done(null, true)

I had a similar issue. Could be due to the express-session middleware needed for passport. Fixed it by using middlewares in the following order: (Express 4) var session = require(‘express-session’); // required for passport session app.use(session({ secret: ‘secrettexthere’, saveUninitialized: true, resave: true, // using store session on MongoDB using express-session + connect store: new MongoStore({ … Read more