[英]How to use two separate passport instances in a single express server file
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.
我创建了一个app.js,其中包含了护照,并创建了一个应用程序以及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. 现在,我已定义了路由来侦听/ auth并处理身份验证。 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. 因此,可以说我有一个
userSessions.js
和adminSessions.js
文件来处理身份验证路由。 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
. 现在,如果我尝试在管理应用程序上进行身份验证,它仍将转到
userSessions.js
的passport.serializeUser
和passport.deSerializeUser
方法。
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?
还是我做错了方法,应该在完全不同的应用程序中处理管理部分?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.