javascript - How to use two separate passport instances in a single express server file - Stack Overflow

I have an express app with a user and admin section. Both will run on different ports and need differen

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 pletely different app?

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 pletely different app?

Share Improve this question asked Feb 14, 2016 at 13:25 Vivek V DwivediVivek V Dwivedi 2,6152 gold badges30 silver badges40 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

Have not tried but you can look for passports

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742323384a4422265.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信