Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I'm new to fullstack apps, and I just managed to implement facebook authentication with passport. Right now I have my schema/models set up properly, and passport is saving facebook profile in mongodb the way I want.

Now I want entries in mongodb to talk to my Angular code. Right now I did a hack in my passport routes, where if there is a req.user (which has a facebook object per mongoose schema), I set that user to rootScope.user in Angular with the usage of cookies as below (see the res.send of the cookie in passport code all the way at the bottom).

var user = $cookies.user;
$rootScope.currentUser = user;

this is obviously not a good idea for scalability. How do I grab the facebook data stored in mongo (thanks to passport), and get Angular to talk with it? I imagine some kind of $http request to a route. Would I make that request to, say '/profile' as below in my passport code? I'm pretty confused and I know that this is a core concept that I need to understand.

// if no error, this gets hit:
app.get('/auth/facebook/callback', function (req, res, next) {

  console.log('on the way back');

  passport.authenticate('facebook', function (err, user, redirectURL) {
    console.log(' in the callback ');
    if (err) {
      return res.redirect('/#!/login');
    }

    if (!user) {
      return res.redirect('/#!/signup');
    }
    if (req.user) {
      console.log(req.user);
      res.cookie('user', JSON.stringify(req.user));
  }
    //actually storing the fact that they are logged in:
    req.login(user, function (err) {
      if (err) {
        return res.redirect('/#!/login');
      }

      return res.redirect('/');
    });
   })(req, res, next);
 });

app.get('/profile', function(req, res, next) {
  // get the data here from Mongo somehow?
});
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.