Pass Express req and res data to client using Reactjs

时间:2015-07-08 15:53:19

标签: javascript node.js express reactjs

I am using ReactJS with client-side routing and attempting to implement a Facebook login using Passport. Everything is fine except I am unsure how to pass the req.user I generate to my React component, as I am not using Handlebars or Jade. Is this possible?

UPDATE

Here is the current code I am using:

app.use('/', express.static(__dirname + '/public'), function(req, res){
  if (req.user){
    res.send(req.user);
  }
});

app.get('/facebook', passport.authenticate('facebook', { scope : 'email' }));

// handle the callback after facebook has authenticated the user
app.get('/facebook/callback',
    passport.authenticate('facebook', {
      successRedirect : '/'
}));

What I don't know how to do is access the req.user in my client-side React component (I am storing authentication and user info via a session with Flux), as any documentation shows loading it up through a Jade or Handlebars template, neither of which I am using. Any ideas?

1 个答案:

答案 0 :(得分:0)

您想将整个req.user暴露给客户端的原因是什么?使用会话的想法是完全从客户端删除该数据,因为客户端总是被假定为不安全的主机。因此,用户数据和会话之间的映射是通过客户端上的cookie安装的sessionId完成的。

过去,我需要客户端在登录后知道其用户名,但没有别的。我通过在客户端使用该值放置cookie来解决该问题。这对你来说是一个可行的方法吗?也就是说,选择客户端需要知道的数据(而不是传递整个用户对象)并将其放在cookie本身中?