passport / discuss

A forum for discussing topics related to the usage of Passport.js.
1 stars 0 forks source link

FacebookStrategy not providing profile object, while providing a valid access token. #36

Closed AndreHBSilva closed 5 years ago

AndreHBSilva commented 5 years ago

I wrote an API that uses Passport and FacebookStrategy to login with a Facebook account. This login flow was working till today when suddenly FacebookStrategy stopped providing the profile object, weirdly providing accessToken. Here's what I coded:

const passport = require('passport');
const User = require('../models/User');
const { Strategy: FacebookStrategy } = require('passport-facebook');
const {
  FACEBOOK_CONFIG,
} = require('../config');

module.exports = () => {

  passport.serializeUser((user, done) => done(null, user.id));
  passport.deserializeUser((id, done) => {
    User.getUserById(id, function(err, user) {
      done(err, user);
    });
  });

  const callback = (accessToken, profile, cb) => {
    console.log(`${accessToken}, ${profile}`);
    User.findOne({ 'facebook.id': profile.id }, (err, user) => {
      if (err) return cb(err);
      if (user) {
        User.findOneAndUpdate({ 'facebook.id': profile.id }, { 'facebook.accessToken': accessToken }, (err) => {
          if(err) console.log(err);
          else console.log('accessToken trocado.');
        });
      }
      else {
        var newUser = new User();
        newUser.facebook.id = profile.id;
        newUser.facebook.token = profile.token;
        newUser.facebook.name = `${profile.name.givenName} ${profile.name.familyName}`;
        newUser.facebook.photoUrl = profile.photos[0].value;
        newUser.facebook.accessToken = accessToken;
        if (typeof profile.emails !== 'undefined' && profile.emails.length > 0)
          newUser.facebook.email = profile.emails[0].value;
          newUser.save((err) => {
            if (err) throw err;
            return cb(null, newUser);
          });
      };
    });
   cb(null, profile);
  };

  passport.use(new FacebookStrategy(FACEBOOK_CONFIG, callback))
}

I really don't know if I did something wrong. Any help is appreciated.

Thanks!

AndreHBSilva commented 5 years ago

Solved it! The callback function must have the 4 parameters (accessToken, refreshToken, profile and cb), not 3 as I was writing it on the code. Hope it helps someone! Peace!