0

How do I run two or more passport strategies sequentially, meaning if one strategy comes up empty then run another one?

I tried doing this:

app.post('/', function (req,res,next){ 
  passport.authenticate('strategy1', function (err, result1) {
    if (err) { return next(err); }
    if (!result1) { 
        passport.authenticate('strategy2', function (err,result2){
           if (err) { return next(err); }
           if(!result2){
             return res.redirect('/');}
           req.login(result2, function (err){
               if(err){return next(err)}
                 res.render('result2');
             })
           });
    }
    req.login(result1, function (err){
        if (err){return next(err)}
          console.log('admin login found');
          res.render('result');
    });
})(req, res, next);
});

But am getting this error:

Error: Failed to serialize user into session

I have implemented:

passport.serializeUser(function(user, done) {
  done(null, user);
});

passport.deserializeUser(function(user, done) {
 done(null, user);
});

I suspect the user was serialised in the first passport.authentication call and then it tried to serialise it again with the second one, and what I need to do is deserialise it again before running the second strategy.

Appreciate the help!

qts
  • 984
  • 2
  • 14
  • 25
  • Can you replace the serialize function with `done(null, JSON.stringify(user))` and the deserializer function with `done(null, JSON.parse(user))`? – Aᴍɪʀ Jan 06 '16 at 19:01

1 Answers1

0

I ended up sticking to just one strategy but added logic to allow it to check several collections, as recommended in this answer.

passport.use('local', new LocalStrategy({
passReqToCallback : true
}, function(req, username, password, done) {
    process.nextTick(function() {
        collection1.findOne({'username': username}, function(err, collectionresult) {
            if (err) {
                return done(err);
            }          
            if (!collectionresult) {
                collection2.findOne({'username': username}, function(err, collection2result){
                     if (err) {
                        return done(err);
                    }          
                    if (!collection2result) {
                        return done(null, false,req.flash('adminmessage','Invalid username or password'));
                    }
                    if (!collection2.validPassword(password))  {
                        return done(null, false,req.flash('adminmessage','Invalid username or password'));
                    }
                    console.log('local strategy has authenticated employee username and password! Returning employee');
                    return done(null, employee);
                })
            }
            if (collection2result){
                if (collection2result.password!=password) {
                return done(null, false, req.flash('adminmessage','Invalid username or password' ));
                }
                else{
                    console.log('Local strategy has found an admin. Returning admin');
                    return done(null, collection2result)
                }
            }
        });
    }
);
}));
Community
  • 1
  • 1
qts
  • 984
  • 2
  • 14
  • 25