How to use two separate passport instances in a single express server file

只愿长相守 提交于 2019-12-11 11:10:03

问题


I have an express app with a user and admin section. Both will run on different ports and need different authentication. I created an app.js where I include passport and create an app as well as adminApp. Then I initialize passport for both as follows:

var passport = require('passport');
var app = express();
var adminapp = express();

adminapp.use(passport.initialize());
adminapp.use(passport.session());

app.use(passport.initialize());
app.use(passport.session());

Now I have routes defined to listen to /auth and handle authentication. Since I am running these on different ports, I have same patterns for routes, and both are listening correctly on their ports. So lets say I have a userSessions.js and adminSessions.js files to handle the authentication routes. These are included as follows:

var userSessions = require('./routes/userSessions');
var adminSessions = require('./routes/adminSessions');

app.use('/auth', userSessions);
adminapp.use('/auth', adminSessions); 

Now if I try to authenticate on the admin app, it still goes to the passport.serializeUser and passport.deSerializeUser methods of userSessions.js.

How can I handle this situation so that admin and user authentication are handled separately? Or am I doing this the wrong way and admin section should be handled in completely different app?


回答1:


Have not tried but you can look for passports

var Passport = require('passport').Passport,
    appPass = new Passport(),
    adminappPass = new Passport();


来源:https://stackoverflow.com/questions/35392317/how-to-use-two-separate-passport-instances-in-a-single-express-server-file

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