2

passport.socketio 抛出此错误,同时未能授权用户。

错误:错误:无法将用户反序列化出会话

我已将问题缩小到 passport.socketio 的问题/lib/index.js

在第 59 行

  auth.passport.deserializeUser(userKey, function(err, user) {
    if (err)
      return auth.fail(data, err, true, accept);
    ...

它抛出该错误。调试器告诉我它userKey是有效的,应该反序列化用户。这与我的主应用程序中的护照用于反序列化用户的密钥相同。(这是 mongoDB 对象的 ID)。我的主应用程序中的护照在反序列化用户时没有问题。(详细信息)所以不知道为什么这仍然会引发错误。

userKey此处传递的密钥与我的主应用程序中用于反序列化的密钥护照相同。

我已经达到了制作userKey全局并将其放入我的主要代码的程度

  passport.deserializeUser(global.userKey, function(err, user) {
    if (err)
      return auth.fail(data, err, true, accept);
    console.log('ok');

这会导致无限循环(因为它在外部 passport.deserialize 内部)但 iut 打印“ok”!所以我的主应用程序中的护照可以使用与 index.js 中的护照相同的东西至少反序列化(passport.socketio\lib\ index.js) 不能!.. 由于某些原因。

然后我什至尝试从主应用程序传递护照对象本身

io.set('authorization', require('passport.socketio').authorize({
    passport: passport,
    ...

这实际上不会导致错误!但后来我没有得到socket.handshake对象。

我没有进一步诊断的想法,并且非常感谢任何帮助。

What could be causing passport.socketio's passport to not "deserialize user out of session"?

4

2 回答 2

3

Deleted npm_modules, re-wrote the packages.json with "every_package":"latest", and so basically re-installed every package's latest version. That fixed it.

于 2014-05-08T14:38:38.690 回答
2

One problem could be that you have configured your 'passport' instance in the main app to use a specific 'deserializeUser' implementation. look for all the places where your passport has been intiallized in the main app. (If its a framework like mean.io, you will find it in config/passport.js).
Make sure the same initiallization is done to the passport instance in the socket app. Pass it to passportsocketio as such:

passportSocketIo.authorize({
        passport: passport,
        cookieParser: express.cookieParser,
        key: 'connect.sid'
        ...
});
于 2014-05-30T20:48:46.960 回答