I am trying to use PassportJs with multiple types

2019-05-21 04:35发布

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

  1. I had to give distinct names for each local strategy
  2. I had to serialize/deserialize depending on the type of the user (client or admin in my case).

2条回答
贪生不怕死
2楼-- · 2019-05-21 04:52

you also want to use separate instances of passport

var Passport = require ('passport');
var passportAdmin = new Passport.Passport();
var passportClient = new Passport.Passport();
查看更多
等我变得足够好
3楼-- · 2019-05-21 05:08

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).

查看更多
登录 后发表回答