How to distinguish Login from registration when using PassportJS for Facebook

I'm trying to set up goals in Google Analytics to track Sign Ups, so I created a thank you page as my goal URL. It works well when my users register with their email address, but not when they use facebook to register / login. When they log in, they are redirected to the thank you page, because when setting up Facebook using Passport JS and Node, there is only one URL callback.

Here is my code:

passport.serializeUser(function(user, done) { done(null, user); }); passport.deserializeUser(function(id, done) { UserActivity.findOne(id,'uid ref', function (err, user) { done(err, user); }); }); passport.use(new FacebookStrategy({ clientID: 'XXXXXXXXXXXX', clientSecret: 'XXXXXXXXXXXXXXXX', callbackURL: "https://www.xxxxxxx.com/auth/facebook/callback" }, function(accessToken, refreshToken, profile, done) { //console.log(profile); User.findOne({ uid: profile.id }, function(err, uInfo) { if(err) console.log('Error: '+err); else{ //User exists: we are done if(uInfo){ done(err, uInfo); } else{ //User doesn't exist: we create a new one var newUser = new User ({ uid: profile.id, email:profile.emails[0].value, ref: 'Facebook' }); // Saving it to the database. newUser.save(function (err,uInfo) { if (err) console.log ('Error on save!'); done(err, uInfo); }); } } }) } )); app.get('/auth/facebook', passport.authenticate('facebook',{ scope: 'email' })); app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/thankyou', failureRedirect: '/login' })); 

If the user exists, I would like to redirect him to the control panel ('/ dashboard), and if they are new users, I need to redirect them to / thankyou. Any idea how to achieve this?

Thanks a lot!

+6
source share
1 answer

Nevermind, found the answer. Below is the updated code. Note the use of passReqToCallback and req.session.newu

 passport.use(new FacebookStrategy( { clientID: 'XXX', clientSecret: 'XXX', callbackURL: "https://www.XXX.co/auth/facebook/callback", passReqToCallback: true }, function(req, accessToken, refreshToken, profile, done) { //console.log(profile); User.findOne({ uid: profile.id }, function(err, uInfo) { if(err) console.log('Error: '+err); else{ if(uInfo){ done(err, uInfo); } else{ var newUser = new User ({ uid: profile.id, email:profile.emails[0].value, ref: 'Facebook' }); // Saving it to the database. newUser.save(function (err,uInfo) { if (err) console.log ('Error on save!'); req.session.newu=true; done(err, uInfo); }); } } }) } )); app.get('/auth/facebook', passport.authenticate('facebook',{ scope: 'email' })); app.get('/auth/facebook/callback',function(req, res, next) { passport.authenticate('facebook', function(err, user, info) { if (err) { return next(err); } if (!user) { return res.redirect('/login'); } req.logIn(user, function(err) { if (err) { return next(err); } var redLink = '/dashboard'; if(req.session.newu)redLink = '/dashboard?newu' return res.redirect(redLink); }); })(req, res, next); }); 

Will the existing user be redirected to / dashboard and the new user will be redirected to the control panel? newu Google Analytics doesnโ€™t need two different URLs, it just needs a query string. When I configure the url, did I select the url start with / dashboard? Newu.

Hope this helps

+6
source

All Articles