NodeJS Passport: multilogin through two strategies - serialize the problem

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.

+4
source share
1 answer

Short answer: use the same type of user object for BOTH so that local and third-party check callbacks. You probably want to set this in your Instagram validation callback: instead of calling done(null,profile) you can create a new User object, assign myUser.instagram = profile and callback done(null, myUser) or something like that.

I have a sample code that demonstrates one way to configure multiple authentication systems. You can find it here: https://github.com/therealplato/passport-multiauth-demo/blob/master/app.js

A couple of warnings: this was written for Express 2.x, which processes the server differently from 3 and has a different syntax for sending responses. edit, demo code updated for express 3.x

Also, the Google method here is Oauth 1. I cannot get Oauth 1 google auth to work on localhost, it gives an error ...has no method 'CharChodeAt'...

Oddly enough, I spent today on getting Oauth 2 to play well with my local users. Therefore, if I find the time, I will try to update the demo code for Express 3 and Oauth 2, but not promises :)

+4
source

All Articles