cb is not a function when using passport for facebook authentication

276 views Asked by At

I'm using passport for facebook authentication using nodejs and reactjs app. I have the following code

// index.js

const session = require("express-session");
const passport = require('passport');

app.use (
      session ({
        secret: "FMfcgzGllVtHlrXDrwtpNdhLRXlNtVzl@18088dda1",
        resave: true,
        saveUninitialized: true,
        cookie: {
            expires: 60 * 60 * 24,
        }
     })
);
app.use(passport.initialize());
app.use(passport.session()); 
require("./passportConfig")(passport);

// in passportConfig.js

const User = require("./models/user");

const FacebookStrategy = require('passport-facebook');

module.exports = function (passport) {

    passport.use(new FacebookStrategy({
            clientID: process.env.FACEBOOK_CLIENT_ID,
            clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
            callbackURL: '/user/facebook/callback',
            profileFields: ['id', 'displayName', 'email', 'name', 'picture'],
            passReqToCallback: true,
            enableProof: true
        },
        (accessToken, refreshToken, profile, cb) => {
            console.dir(profile);
            // save the profile on the Database
            // Save the accessToken and refreshToken if you need to call facebook apis later on
            return cb(null, profile);
        }));


    passport.serializeUser((user, cb) => {
        cb(null, user.id);
    });

    passport.deserializeUser((id, cb) => {
        User.fetchById(id).then(result => {
            cb(null, result[0]);
        }).catch(err => {
            cb(err, null);
        });
    });

};

I get the following error when running the code "cb is not a function". I checked the documentation, and this should work. What am I doing wrong?

1

There are 1 answers

0
David On

I faced the same issue. Found a workaround by passing an extra argument to the verify callback. There is some function arity check inside the library to pass extra params to the verification callback:

passport.use(new FacebookStrategy({
        clientID: process.env.FACEBOOK_CLIENT_ID,
        clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
        callbackURL: '/user/facebook/callback',
        profileFields: ['id', 'displayName', 'email', 'name', 'picture'],
        passReqToCallback: true,
        enableProof: true
    },
    (accessToken, refreshToken, params, profile, cb) => {
      console.log("params: " +params);
      console.log(profile);
      return cb(null, profile);
    }));