问题
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