1
votes

I'm working with nodejs+express+mongo and angularjs(MEAN). I'm login in with Facebook strategy for passport but I can't seem to find the way to access the user info(req.user) from my angular client. It should be noted that the angular front is on another app and web server, apart from the node express mongo side.

This is my nodejs code:

      passport.serializeUser(function(user, done){
          done(null, user._id);
        });

      passport.deserializeUser(function(id, done){

        UserModel.findById(id, function(err, user){
          done(err, user);
        });
      });

      passport.use(new FacebookStrategy({
          clientID: config.facebook.appId,
          clientSecret: config.facebook.appSecret,
          callbackURL: config.facebook.redirectUri,
          scope: config.facebook.scope,
          profileFields: ['name','displayName','gender','profileUrl','email']

        },
        function(accessToken, refreshToken, profile, done) {

          UserModel.findOne( {$or:[{'facebook_profile.id': profile.id},{'email':profile.emails[0].value}]}, function(err, user){
            var newuser = new UserModel();
            if(err){

              return done(err);
            }
            if(user){
               return done(null, user);
            }
            else {


              newuser.facebook_profile.id = profile.id;
              newuser.facebook_profile.name = profile.displayName

              return done(null, newuser);



            }
          });
        })
      );

        app.get('/auth/is_auth', function(req, res){

          if (req.isAuthenticated()){
            res.json(req.user);

          }
        })

After this, from Postman I call http://localhost:3000/auth/is_auth

But req.isAuthenticated() is always FALSE

What can it be, I can't never get the user info

1

1 Answers

1
votes

req.isAuthenticated() is false, if you use CORS and in your ajax request you didn't set withCredentials to true. Be sure withCredentials is set to true.