title: PassportJS - Using multiple passports within Express application
body: <p>Suppose I need two different passports within one express application (e.g. user &amp; room).
  So I define two separate vars:</p>
  
  <pre><code>var passport    = require('passport');
  var roomPassport = require('passport');
  </code></pre>
  
  <p>then, I initialize them with separate passport strategies:</p>
  
  <pre><code>require('./config/passport')(passport); // pass passport for configuration
  require('./config/roompassport')(roomPassport); // pass passport for configuration
  </code></pre>
  
  <p>the last step is to set them as Express middleware:</p>
  
  <pre><code>// 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`
  </code></pre>
  
  <p>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.</p>
  
  <p>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 <code>req.user</code> and <code>req.room</code> have to exist.</p>
  
  <p>How to resolve this?</p>
  
  <p>EDIT 1</p>
  
  <p>Well, after few more hours, it seems that although I have to separate passport objects, after the call of <code>application.use(roomPassport.initialize({ userProperty: "room" }));</code>, things get messy - and this is because <code>req.login()</code> method works with the last attached passport. So instead of calling the correct <code>passport.serializeUser</code> method, it calls <code>roomPassport.serializeUser</code> method.</p>
  
  <p>My next question - how to make <code>req.login()</code> to call the right method?</p>