Error with Facebook redirect URL on login/register using passport on a node.js (MEAN stack) application

571 views Asked by At

I am implementing the Facebook login and register functions to my application, and can reach facebook for the user to enter their information, but can't make the callback work. I have input the code below into passport.js (obviously x'ing out our information):

passport.use(new FacebookStrategy({
clientID: "xxxxxxxxxxxxxx",
clientSecret: "xxxxxxxxxxxxxxxxxxx",
callbackURL: "https://xxxxxxxxxxxxxxxx/auth/facebook/callback/",
enableProof: false,
profileFields: ['id', 'displayName', 'photos']
},
function (accessToken, refreshToken, profile, done) {
 process.nextTick(function () {

 User.findOne({facebookId: profile.id}, function (err, user) {

  if (err) {
    return done(err);
  }

  if (user) {

    return done(null, user);

  } else {

    var data = {
      facebookId: profile.id,
      f_name: profile.first_name,
      l_name: profile.last_name,
      username: profile.email
    };

    if (profile.emails && profile.emails[0] && profile.emails[0].value)            {
      data.username = profile.emails[0].value;
    }

    User.create(data, function (err, user) {
      return done(err, user);
    });
  }
});
});
}));

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

passport.deserializeUser(function(id, done) {
 User.findById({
 _id: id
 }, function(err, user) {
  callback(err, user);
 });
});  

passport.use(new LocalStrategy(
 function (username, password, done) {
   console.log(username);
   console.log(password);
User.findOne({ 'username': username }, function (err, user) {
  // if (err) { return done(err); }
  if (!user) {
    return done(null, false, { message: 'Incorrect email.' });
  }
  if (!user.validPassword(password)) {
    return done(null, false, { message: 'Incorrect password.' });
  }
  return done(null, user);
});
 }
));

The following callback URL has been included in index.js:

   router.get('/auth/facebook', passport.authenticate('facebook', { 
   scope: 'email' }));

 router.get('/auth/facebook/callback',
    passport.authenticate('facebook', {
        successRedirect : '/user/#/home',
        failureRedirect : '/'
    }));

I am able to make the application direct to the Facebook login page onClick, but once the user enters their email and password into facebook, the application is unable to reload on redirect and get the person to our homepage. The redirect begins to happen, but hits an error once it tries to load our application. Could we be utilizing one of the redirect fields incorrectly? As a side note, what should we set as a valid OAuth Redirect URI on the facebook developer application page?

0

There are 0 answers