I am trying to use PassportJs with express using multiple types of users (each one has a different model). When I use it with only one type of user it is OK but when I add a second one it is not working. Any idea ?
// Configuring Passport
var passport = require ('passport');
var LocalStrategyAdmin = require ('passport-local').Strategy;
var LocalStrategyClient = require ('passport-local').Strategy;
var adminModel = require ('./models/adminModel');
var clientModel = require ('./models/clientModel');
var session = require ('cookie-session');
app.use (session ({keys: [secretKey1, secretKey2]}));
app.use (passport.initialize ());
app.use (passport.session ());
passport.use (new LocalStrategyAdmin (adminModel.authenticate ()));
passport.use (new LocalStrategyClient (clientModel.authenticate ()));
passport.serializeUser (adminModel.serializeUser ());
passport.deserializeUser (adminModel.deserializeUser ());
passport.serializeUser (clientModel.serializeUser ());
passport.deserializeUser (clientModel.deserializeUser ());
I fixed it
- I had to give distinct names for each local strategy
- I had to serialize/deserialize depending on the type of the user (client or admin in my case).