Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to differentiate Login from Sign up when using PassportJS for Facebook

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

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 to their dashboard ('/dashboard) and if they are new users, I need to redirect them to /thankyou. Any idea how to achieve this?

Thanks a lot!

like image 782
Cyril Gaillard Avatar asked Jan 03 '14 06:01

Cyril Gaillard


1 Answers

Nevermind, found the answer. Here is the updated code below. Pay attention to 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);

});

An existing user will be redirected to /dashboard and a new user will be redirected to /dashboard?newu Google Analytics doesn't need 2 different urls, it just needs a query string. When I set up the url goal, I selected url start with /dashboard?newu.

Hope this helps

like image 137
Cyril Gaillard Avatar answered Sep 28 '22 19:09

Cyril Gaillard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!