PassportJS - 在Express应用程序中使用多个护照 [英] PassportJS - Using multiple passports within Express application

查看:115
本文介绍了PassportJS - 在Express应用程序中使用多个护照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在一个快速申请(例如用户和房间)内需要两张不同的护照。
所以我定义两个单独的vars:

  var passport = require('passport'); 
var roomPassport = require('passport');

然后,使用单独的护照策略初始化它们:



($。p $ p> require('./ config / passport')(护照); //通过护照配置
require('./ config / roompassport')(roomPassport); //通过护照配置

最后一步是将其设置为Express中间件:

  //护照需要
application.use(session({secret:'ilovepassport ;-)'})); // session secret
application.use(passport.initialize({userProperty:user}));
application.use(passport.session()); //持久登录会话
application.use(roomPassport.initialize({userProperty:room}));
application.use(roomPassport.session()); //持久登录会话
application.use(flash()); //使用connect-flash存储在会话中的闪存消息

但是,如果我这样做实际上,roomPassport覆盖了护照,而不是拥有两个对象 - req.user和req.room,我只有一个req.room,但用户数据初始化。



重要的是要提到每个护照(用户或房间)可以彼此独立地进行身份验证,即有两种对象 req.user

如何解决这个问题?



编辑1



嗯,经过几个小时,似乎虽然我必须分开护照对象,在调用 application.use(roomPassport .initialize({userProperty:room})); ,事情变得凌乱 - 这是因为 req.login()最后附上的护照。所以不要调用正确的 passport.serializeUser 方法,它调用 roomPassport.serializeUser 方法。



我的下一个问题 - 如何使 req.login()调用正确的方法?

解决方案

您需要的问题是护照模块在需要时导出实例化的Passport对象。而且因为这个对象在需要的时候被节点缓存,所以每次都会得到完全相同的对象。



幸运的是,护照模块还给​​你一个引用类,这意味着你可以这样做。

  var Passport = require('护照')护照,
护照=新护照(),
roomPassport =新护照() ;

现在您应该有两个完全独立的护照对象。


Suppose I need two different passports within one express application (e.g. user & room). So I define two separate vars:

var passport    = require('passport');
var roomPassport = require('passport');

then, I initialize them with separate passport strategies:

require('./config/passport')(passport); // pass passport for configuration
require('./config/roompassport')(roomPassport); // pass passport for configuration

the last step is to set them as Express middleware:

// required for passport
application.use(session({ secret: 'ilovepassport;-)' })); // session secret
application.use(passport.initialize({ userProperty: "user" }));
application.use(passport.session()); // persistent login sessions
application.use(roomPassport.initialize({ userProperty: "room" }));
application.use(roomPassport.session()); // persistent login sessions
application.use(flash()); // use connect-flash for flash messages stored in session`

however, if I did it like this, in fact roomPassport overrides passport and instead of having two objects - req.user and req.room, I have got just one req.room but initialized with user data.

It is important to mention that each passports (user or room) could authenticate independently from each other, i.e. there is a scenario where both objects req.user and req.room have to exist.

How to resolve this?

EDIT 1

Well, after few more hours, it seems that although I have to separate passport objects, after the call of application.use(roomPassport.initialize({ userProperty: "room" }));, things get messy - and this is because req.login() method works with the last attached passport. So instead of calling the correct passport.serializeUser method, it calls roomPassport.serializeUser method.

My next question - how to make req.login() to call the right method?

解决方案

The problem your having is that the passport module exports an instantiated Passport object when you require it. And because that object is cached by node when you require it, you get the exact same object every time.

Luckily, the passport module also gives you a reference to the class, meaning you can do this.

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

Now you should have two completely separate passport objects.

这篇关于PassportJS - 在Express应用程序中使用多个护照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆