【问题标题】:Why is my req.user always null on load?为什么我的 req.user 在加载时总是为空?
【发布时间】:2013-04-17 14:25:44
【问题描述】:

我已经苦苦挣扎了几天,并且取得了一些不错的进展,但我无法让我的会话内容完全正常工作。

我已成功使用 Passport 通过 Facebook 进行身份验证。当我使用 FB 按钮单击登录时,会话已完美加载,并且我的 req.user 对象已准备就绪,但在我看来,我应该只需要这样做一次。

我知道护照会保存一个 cookie。我查了一下,发现它工作正常。

我正在尝试检测用户是否已经在加载我的索引页面时登录,但是当我加载页面时,req.user 对象始终为空,并且我的 passport.deserializeUser 方法永远不会被调用来加载它(如果我点击登录 FB 按钮,它会被调用)。

所以我的问题是,如何在页面加载时告诉护照检查 cookie 并加载用户会话(如果有的话)?


更新 - 好的,对于那些以后发现这个问题的人,我只是想复习一下我在这里学到的东西(感谢那些发表评论的人)。希望它会帮助其他人。

我习惯了 .NET cookie,它与服务器无关。 Node.js 和护照不在同一个前提下工作。默认情况下,node.js 使用内存存储来保持其会话。当您在终端/命令行中关闭节点时(每次更改服务器代码时都必须这样做),memorystore cookie 信息会被重置,因此与之相关的任何 cookie 都没有意义。

为了解决这个问题,我安装了 Redis (http://cook.coredump.me/post/18886668039/brew-install-redis) 并将其连接为我的存储 (http://www.hacksparrow.com/use-redisstore-instead-of-memorystore-express-js-in-production.html)。

这将在我计划的生产服务器 Azure 上运行,所以一切都很好。


好的,这里有一些代码。我不确定要放置哪些部分...

这是 server.js

/**
* Module dependencies.
*/

var express = require('express')
, app = express()
, partials = require('express-partials')
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server)
, routes = require('./routes')
// facebook
, passport = require('passport')
, facebookStrategy = require('passport-facebook').Strategy
// custom stuff
, urlCommand = require('./middleware/UrlCommand')
, azureCommand = require('./middleware/AzureCommand')
, userCommand = require('./middleware/UserCommand');

// Ports
var port = process.env.port;

if (isNaN(port))
  port = 3000;

server.listen(port);

//allows the use of Layouts
app.use(partials());

passport.serializeUser(function(user, done) {
  done(null, user.RowKey);
});

passport.deserializeUser(function (id, done) {
    console.log("deserialize");
    userCommand.findByID(id, function (err, user) {
        done(err, user);
    });
});

// Configuration
app.configure(function () {
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.cookieParser());
    app.use(express.bodyParser());
    app.use(express.session({ secret: 'SECRET!' }));
    app.use(express.methodOverride());
    app.use(passport.initialize());
    app.use(passport.session());  
    app.use(app.router);
    app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

// Facebook
passport.use(new facebookStrategy({
    clientID: CLIENT,
    clientSecret: "SECRET",
    callbackURL: "http://localhost:3000/auth/facebook/callback" //DEV
},
  function (accessToken, refreshToken, profile, done) {
      userCommand.findOrCreate(profile.id, profile.name.givenName,   profile.name.familyName, profile.emails[0].value, accessToken, function (error, user) {
          return done(null, user);
      });
  }
));

// Routes
app.get('/', routes.index);
app.get('/auth/facebook', passport.authenticate('facebook', { scope: 'email' }));
app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect:   '/',
                                                                   failureRedirect: '/login' }));

// Sockets
io.sockets.on('connection', function (socket) {
    // when the client emits 'sendURL', this listens and executes
    socket.on('sendURL', function (data) {
        console.log('sendURL called: ' + data);
        urlCommand.AddURL(data);

        // we tell the client to execute 'processURL'
        io.sockets.emit('urlComplete', data);
    });
});

console.log("Express server listening on port %d in %s mode", port, app.settings.env);

index.js

exports.index = function (req, res) {
    console.log(req.user); // ALWAYS NULL
    res.render('index', { title: 'Express' })
};

用户命令

var azureCommand = require('../middleware/AzureCommand');
var tableService = azureCommand.CreateTableService();

function findByID(id, callback) {
    console.log('FindByID');

    tableService.queryEntity('user', 'user', id, function (error, entity) {
        console.log('Found him: ' + entity.Email);
        callback(error, entity);
    });
}

function findOrCreate(id, first, last, email, accessToken, callback) {
    var user = {
        PartitionKey: 'user'
        , RowKey: id
        , First: first
        , Last: last
        , Email: email
        , AccessToken: accessToken
   }

    tableService.insertEntity('user', user, function (error) {
        callback(null, user);
    });
}

exports.findByID = findByID;
exports.findOrCreate = findOrCreate;

这是我输出会话时显示的输出日志...

node server.js
info  - socket.io started
Express server listening on port 3000 in development mode
{ cookie:
   { path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: true },
  passport: {} 
}
debug - served static content /socket.io.js

【问题讨论】:

  • 听起来您的会话 cookie 的过期时间可能不正确,但请显示代码(尤其是您的应用程序和 Passport 配置),否则我只是在猜测。
  • 没有代码就不可能告诉你出了什么问题。你查过example 自带护照-facebook
  • 你写 "如果我点击登录到 FB 按钮,它确实会被调用" 关于deserializeUser。当您调用该按钮时,会调用哪条路由?而deserializeUser 从来没有被称为/ 路由,或者它只是不起作用?由于您没有处理 FacebookStrategy 回调中的错误,这可能是个问题吗?
  • 嘿罗伯特,我不认为这个问题与 FB 有关。这似乎工作正常。当我单击授权按钮时,我可以进行身份​​验证并让我的会话正常工作。问题是当用户返回并且已经验证并将其存储为 cookie 时。我希望网站在 / 路线上识别它们。目前这不起作用......没有任何东西被调用来设置它,我不知道如何强制它。
  • 您将user.RowKey 存储在会话中,因此deserializeUser 将使用该行键作为ID 调用findById;你的数据库是这样工作的吗?

标签: node.js session express passport.js


【解决方案1】:

好吧,首先你应该发布一些代码,因为问题可能出在任何地方。

比查看漂亮的文档http://passportjs.org/guide/

尽管passport通过github给你带来了很多例子。

如果您想拥有一个具有不同身份验证方法的完整示例,您应该访问https://github.com/madhums/nodejs-express-mongoose-demo

【讨论】:

    【解决方案2】:

    问题出在您的 serializeUserdeserializeUser 函数中。

    正如您所注意到的,当您单击 FBlogin 按钮时,deserializeUser 是第一次也是唯一一次调用 - 实际上它是使用 id 调用的,而该 serializeUser 函数之前已返回该函数。如果此时它无法通过提供的id 找到用户,passport.user 将不会保存到会话中。

    因此,在以下请求中根本不会调用 passport.deserializeUser,因为 express.session 没有使用护照的用户 ID 填充 req.session

    总结:你需要检查你的serializeUser是否返回一个id,而不是你的deserializeUser可以理解和反序列化。

    仅供参考:经过身份验证的用户的正确 req.user 对象应如下所示:

    { 
       cookie: { path: '/', _expires: null,  originalMaxAge: null, httpOnly: true },
       passport: { user: 51772291b3367f0000000001 } 
    }
    

    【讨论】:

    • 所以我输入了一个console.log 来查看我的serialize 方法是否被调用……除非我点击我的FB 按钮。当我单击 FB 按钮时,它被调用,然后我的反序列化被调用,一切正常。另一个实验告诉我,如果我关闭浏览器并返回会话仍然有效。如果我重新启动服务器,它就消失了。这让我觉得我的 cookie 不知何故是基于会话的。如何更新 cookie 护照,使其持续时间超过一个会话?
    • 仅供参考:这是我的 cookie 的输出。 Cookie: {"cookie":{"originalMaxAge":35999999,"expires":"2013-04-25T08:48:38.994Z","httpOnly":true,"path":"/"},"passport": {“用户”:“775119337”}}。我根据这篇文章向它添加了 MaxAge,但它没有帮助。 stackoverflow.com/questions/15016551/…
    • @David 因为你没有使用会话存储,Express 将默认使用MemoryStore 来存储会话,这意味着一旦你的服务器重新启动,会话就消失了。您应该使用持久存储(如connect-mongo),或者使用cookie-based sessions
    • @robertklep 所以 cookie 也被吹走了?我知道我的会话会被吹走,但我认为,默认情况下,cookie 会在下一次加载时出现。没有?
    • @David cookie 应该保持不变,但它的内容将毫无意义(cookie 不是存储 id/RowKey 的地方,它存储在会话对象中;cookie 将只包含一个键从会话存储中检索会话对象)
    猜你喜欢
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 2013-03-15
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多