I would like to save the user object within the request object returned from facebook after successful authentication using passport to use in a different method. The purpose for this is to include in my linkschema all different users who post via the same link.

The flow should be as follows:

  1. facebook authenticate
  2. user object is stored somewhere [HOW DO I DO THIS PART???]
  3. link is clicked, gets routed to post method whose parameters are the link and userid. (If the link exists, the users are appended to the array of users defined in my linkschema)

/ =====================================
/ / FACEBOOK ROUTES
// =====================================
// route for facebook authentication and login
passport.use(new FacebookStrategy({
    clientID: configAuth.facebookAuth.clientID,
    clientSecret: configAuth.facebookAuth.clientSecret,
    callbackURL: "http://localhost:3000/auth/facebook/callback/"
  },
  function(accessToken, refreshToken, profile, done) {
    UserSchema.AddUnique(profile, accessToken, function(err, user) {
      if (err) {
        return done(err);
      }
      return done(null, user);
    });
  }
));

// Redirect the user to Facebook for authentication.  When complete,
// Facebook will redirect the user back to the application at
//     /auth/facebook/callback
router.get('/auth/facebook', passport.authenticate('facebook', {
  scope: 'email'
}));

// Facebook will redirect the user to this URL after approval.  Finish the
// authentication process by attempting to obtain an access token.  If
// access was granted, the user will be logged in.  Otherwise,
// authentication has failed.
var user = router.get('/auth/facebook/callback',
  passport.authenticate('facebook', {
    failureRedirect: '/login'
  }), function(req, res) {
    var user = req.user;
    res.redirect('/browse');
    return function() {
      return user;
    }();
  });


function user() {
  console.log('in here');
  console.log(user);
}

router.use(function(err, req, res, next) {
  console.log(err)
  next(err)
});

router.get('/logout', function(req, res) {
  req.logout();
  res.redirect('/');
});

Thank you in advance!

1

There are 1 answers

0
laggingreflex On

Store them outside in an object

var Users = {};


passport.use(new FacebookStrategy({…},
  function(accessToken, refreshToken, profile, done) {
    UserSchema.AddUnique(profile, accessToken, function(err, user) {
      if (err) {
        return done(err);
      }

      // Push the user into that object
      Users[user.id] = user;

      return done(null, user);
    });
  }
));


function user() {
    console.log(Users) //=> { '5234…': {…} , '5345…': {…} , … }
}