Question

In my application, I'm trying to add loggin with facebook account. As I'm using passport for the moment with local strategy, I tried to add the facebook strategy. I registered on facebook developper site to have my token and secret. I simply copy/paste source code portions from the official passport github, adapting to my personal use. The problem occurs when calling

passport.authenticate('facebook');

I am stuck in here and not redirected to the facebook loggin page. My application is waiting for a reponse or waits to be redirected, but nothing happens. I tried to provide on the facebook developper page my callback URL, and I tried without it (passing the callback throw the passport strategy).

What am I missing ?

My application:

app.get('/auth/facebook', user_routes.facebookLogin);
app.get('/auth/facebook/callback', user_routes.facebookCallback);

My routes:

exports.facebookLogin = function(req, res, next) {
      console.log('Try to login with facebook');
      passport.authenticate('facebook'); //<---------------- does not go further than here
};

exports.facebookCallback = function(req, res, next){
    passport.authenticate('facebook', { 
        successRedirect: '/home',
        failureRedirect: '/login' });
};

My strategy:

passport.use(new FacebookStrategy({
    clientID: "xxxxxxxx",
    clientSecret: "xxxxxxxxxxx",
    callbackURL: "http://localhost:8080/auth/facebook/callback"
  },
  function(accessToken, refreshToken, profile, done) {
      User.findOne({ username: profile.displayName, email: profile.emails[0].value }, function(err, olduser) {
           if (err) { return done(err); }
           if (olduser) { 
               return done(null, olduser); 
           }
           else{
               var newuser = new User({
                   username: profile.displayName, 
                   email: profile.emails[0].value
               }).save(function(err,newuser){
                  if(err) console.log(err);
                  done(null,newuser);
               });
           }

      });
  })
);

[EDIT]

Changing the routing for the callback to this solves my issue. However, I dont understand the reason why...

app.get('/auth/facebook/callback', 
  passport.authenticate('facebook', { failureRedirect: '/login' }),
  function(req, res) {
    res.redirect('/home');
  });
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top