I am trying to use PassportJs with multiple types of users (each one has a different model). What am I doing wrong?

ぃ、小莉子 提交于 2019-12-22 17:55:27

问题


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

回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!