I would like to give the user the opportunity to log in using one of two strategies: through Instagram and the usual local strategy. For local, I wrote a simple function to find the user in the database and provide him with user information. Here's how it works:
passport.use(new InstagramStrategy({ clientID: global.INSTAGRAM_CLIENT_ID, clientSecret: global.INSTAGRAM_CLIENT_SECRET, callbackURL: "http://**.**.**.**:3000/auth/instagram/callback" }, function(accessToken, refreshToken, profile, done) { process.nextTick(function () { console.log('Access token: ' + accessToken); global.access_token = accessToken; return done(null, profile); }); } )); passport.use(new LocalStrategy( function(email, password, done) { console.log('chec user'); process.nextTick(function() { db.findByEmail(email, function(err, user) { if (!user) { console.log('Unknown user ' + email); return done(null, false, { message: 'Unknown user ' + email }); } if (user.password != crypt.getMD5fromString(password)) { console.log('Invalid password'); return done(null, false, { message: 'Invalid password' }); } return done(null, user); }) }); }));
However, my custom functions serialize and deSerialize do not work with Instagram strategy:
/* THESE FUNCTIONS WORK WITH INSTAGRAM STRATEGY passport.serializeUser(function(user, done) { done(null, user); }); passport.deserializeUser(function(obj, done) { done(null, obj); });*/ /*THESE ARE FOR LOCAL STRATEGY*/ passport.serializeUser(function(user, done) {done(null, user._id);}); passport.deserializeUser(function(id, done) {db.findById(id, function(err, user) {done(err, user);});});
How to avoid this error? My goal is to provide an Instagram login and after a successful login check - if the user has a local account (via email or Instagram alias), get the data from the database. Although the user should be able to log in without Instagram using a local strategy. Thanks.