问题
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).
回答1:
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).
回答2:
you also want to use separate instances of passport
var Passport = require ('passport');
var passportAdmin = new Passport.Passport();
var passportClient = new Passport.Passport();
来源:https://stackoverflow.com/questions/35751214/i-am-trying-to-use-passportjs-with-multiple-types-of-users-each-one-has-a-diffe