如何在单个快递服务器文件中使用两个单独的通行证实例 [英] How to use two separate passport instances in a single express server file
问题描述
我有一个带有用户和管理部分的快速应用程序.两者将在不同的端口上运行,并且需要不同的身份验证.我创建了一个app.js,其中包含了护照并创建了一个应用程序以及adminApp.然后我为这两种方式初始化护照:
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());
现在,我已定义了路由来侦听/auth并处理身份验证.由于我在不同的端口上运行这些路由,因此路由的模式相同,并且两者都在其端口上正确侦听.因此,可以说我有一个userSessions.js
和adminSessions.js
文件来处理身份验证路由.这些内容包括:
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);
现在,如果我尝试在管理应用程序上进行身份验证,它仍将转到userSessions.js
的passport.serializeUser
和passport.deSerializeUser
方法.
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?
推荐答案
尚未尝试,但您可以寻找 护照
Have not tried but you can look for passports
var Passport = require('passport').Passport,
appPass = new Passport(),
adminappPass = new Passport();
这篇关于如何在单个快递服务器文件中使用两个单独的通行证实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!