3

I'm incorporating Facebook authentication in my app using Passport with Node and Express, and MySQL db (using Bookshelf.js ORM). When I go to log in using Facebook, after entering my Facebook credentials, I get the error 'cannot get /auth/facebook/callback?code= {callback_code}'. I've tried the following solutions I found on stackoverflow: 1, 2, 3, 4

And this outside post: 5

None of these have worked.

In my FB app account, I've set 'Valid OAuth redirect URIs' inside of Client OAuth Settings to 'localhost:8080/auth/facebook/callback', App Domain to 'localhost', and Site URL to 'localhost:8080/'.

Below is my code. Any help is much appreciated!

Routes:

    module.exports = function (apiRouter, passport) {
      apiRouter.get('/events', isLoggedIn, eventController.getAllEvents);
      apiRouter.post('/events', eventController.addEvent);
      apiRouter.get('/auth/facebook', passport.authenticate('facebook', {scope : ['email ', 'public_profile', 'user_friends']}));
      apiRouter.get('/auth/facebook/callback',
        passport.authenticate('facebook', {failureRedirect: '/'}))
    };

passport config:

passport.use(new FacebookStrategy({
  clientID        : configAuth.facebookAuth.clientID,
  clientSecret    : configAuth.facebookAuth.clientSecret,
  callbackURL     : 'http://localhost:8080/auth/facebook/callback'
},
function(token, refreshToken, profile, done) {
  console.log('looking for user from fb');
  process.nextTick(function() {
    console.log('looking for user from fb inside async');
    new User({ 'facebook_id' : profile.id })
      .fetch()
      .then(function(userModel) {
        if (userModel) {
          return userModel;
        } else {
          new User({
            facebook_token: token,
            first_name: profile.name.givenName,
            last_name: profile.name.familyName
          }).save()
            .then(function(model) {
              console.log('New user saved', model);
              done(null, model);
            },function(error) {
              console.log('Error saving new user: ', error);
              done(error);
            });
         }
         done(null, userModel);
       }, function(error) {
         console.log('Error: ', error);
         done(error);
       });
   });
}));
Community
  • 1
  • 1
Delphine
  • 31
  • 5

0 Answers0