由于连接不使用parseCookie方法了,怎么能使用前preSS我们得到的会话数据? [英] Since connect doesn't use the parseCookie method anymore, how can we get session data using express?

查看:84
本文介绍了由于连接不使用parseCookie方法了,怎么能使用前preSS我们得到的会话数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在node.js中和前preSS,有很多例子说明如何获取会话数据。

正如你可以看到,当您访问的第三个环节,这是到计算器的链接。有一个很好的答案,但作为由@UpTheCreek这些评论所指出的,连接不再具有parseCookie方法。我刚才碰到这个问题为好。所有的教程中,我发现使用连接现在不存在的 parseCookie 方法。于是我问他怎样才能获取会话数据,他说,他不知道,最好的办法,所以我想我会在这里发表的问题。当使用 ex$p$pss@3.0.0rc4 socket.io 的Redis ,我们怎么能得到会话数据和使用授权的用户?我已经能够使用要求(连接)utils.parseSignedCookie; ,但是当我握手时做到这一点,我总是得到一个警告/错误,

 警告 - 握手错误错误

和从我读过这听起来像这不是一个永久性的解决方案反正。



更新

好吧我得到了 session.socket.io 我的服务器上运行。正如我怀疑,我被困在授权点。我想我可能会去对这个错误的方式,可以随意指正。在我的Redis数据库中,我将用户的信息。第一次,他们登录,我要那么它包含的用户信息来更新自己的cookie。那么接下来他们回来到现场的时候,我要检查他们是否有一个cookie,如果用户信息是存在的。如果它不存在,我想送他们到登录界面。在登录屏幕上,当用户提交的信息,这将考验对Redis的数据库信息,如果它匹配,这将更新用户信息的Cookie。我的问题是:

1)如何更新/通过RedisStore改变一个cookie?

2),它看起来像会话数据只在cookie中保存。我怎么能跟踪用户信息过多的页面,如果有人有关闭Cookie?

下面是我的适用code:

  // ...隐藏unapplicable code ...
VAR = Redis的需要('socket.io/node_modules/redis');
变种客户= redis.createClient();
VAR RedisStore =要求('连接-Redis的')(前preSS);
VAR redis_store =新RedisStore();
VAR cookieParser =前press.cookieParser('秘密');app.configure(函数(){
  //...hiding unapplicable code ...
  app.use(cookieParser);
  app.use(如press.session({秘密:'秘密',店:redis_store}));
});//...hiding code启动服务器和socket.ioVAR SessionSockets =要求('session.socket.io');
VAR ssockets =新SessionSockets(IO,redis_store,cookieParser);io.configure(函数(){
  io.set('授权',功能(握手,回调){
    如果(handshake.headers.cookie){
      // VAR饼干= parseCookie(handshake.headers.cookie);
      //如果(cookie.user){
      // handshake.user = cookie.user;
      //}
    }
    回调(NULL,TRUE);
  });
});ssockets.on('连接',函数(ERR,插座,会话){...});


解决方案

切换到 session.socket.io 一会儿我跑进由于几个问题后加载会话信息时,该模块的异步特性。所以,我结束了创建我叫 session.io 自己的模块。它用于这样的:

  VAR前preSS =要求('前preSS');
VAR应用=前preSS();
VAR服务器=要求('HTTP')createServer(应用程序)。//设置cookie和session处理
//注:sessionStore可以使用具有.load()函数的任何sessionStore模块
//但我个人使用模块'sessionstore'来处理我的sessionStores。
VAR cookieParser =前press.cookieParser('秘密');
VAR sessionStore =要求('sessionstore')createSessionStore()。app.configure(函数(){
  app.set('口',process.env.PORT || 3000);
  //...截短...//
  app.use(cookieParser);
  //确保使用相同的秘密,你在你的cookieParser规定
  app.use(如press.session({秘密:'秘密',店:sessionStore}));
  app.use(app.router);
});app.get('/',函数(REQ,RES){
  res.send('&LT;脚本src=\"/socket.io/socket.io.js\"></script><script>io.connect();</script>Connected');
});server.listen(app.get('端口'),功能(){
  的console.log('侦听端口+ app.get('端口'));
});VAR IO =要求('socket.io')听(服务器)。io.configure(函数(){
  //使用session.io得到我们的会话数据
  io.set('授权',需要('session.io')(cookieParser,sessionStore));
});io.on('连接',函数(插座){
  //现在我们有我们的会话数据的访问,像这样
  VAR会话= socket.handshake.session;
  的console.log(会话);
});

In node.js and express, there are many examples showing how to get session data.

As you can see when you visit the 3rd link, it's a link to StackOverflow. There was a good answer, but as pointed out in those comments by @UpTheCreek, connect no longer has the parseCookie method. I have just run into this problem as well. All of the tutorials I have found uses connect's parseCookie method which now doesn't exist. So I asked him how we can get the session data and he said he doesn't know the best approach so I thought I'd post the question here. When using express@3.0.0rc4, socket.io, and redis, how can we get session data and use that to authorize the user? I've been able to use require('connect').utils.parseSignedCookie;, but when I do that, I always get a warning/error when handshaking,

warn - handshake error Error

and from what I've read it sounds like that isn't a permanent solution anyways.


UPDATE

Ok I got session.socket.io working on my server. And as I suspected, I got stuck at the point of authorizing. I think I might be going about this the wrong way, so feel free to correct me. In my Redis database, I will have user's information. The first time that they login, I want to update their cookie so it contains their user information. Then the next time they come back to the site, I want to check if they have a cookie and if the user information is there. If it is not there, I want to send them to the login screen. At the login screen, when a user submits information, it would test that information against the Redis database, and if it matches, it would update the cookie with user information. My questions are these:

1) How can I update/change a cookie through RedisStore?

2) It looks like session data is saved only in cookies. How can I keep track of user information from page to page if someone has cookies turned off?

Here is my applicable code:

//...hiding unapplicable code...
var redis = require('socket.io/node_modules/redis');
var client = redis.createClient();
var RedisStore = require('connect-redis')(express);
var redis_store = new RedisStore();
var cookieParser = express.cookieParser('secret');

app.configure(function(){
  //...hiding unapplicable code...
  app.use(cookieParser);
  app.use(express.session({secret: 'secret', store: redis_store}));
});

//...hiding code that starts the server and socket.io

var SessionSockets = require('session.socket.io');
var ssockets = new SessionSockets(io, redis_store, cookieParser);

io.configure(function(){
  io.set('authorization', function(handshake, callback){
    if(handshake.headers.cookie){
      //var cookie = parseCookie(handshake.headers.cookie);
      //if(cookie.user){
      //  handshake.user = cookie.user;
      //}
    }
    callback(null, true);
  });
});

ssockets.on('connection', function(err, socket, session){ ... });

解决方案

After switching to session.socket.io for a while I ran into a few problems due to the asynchronous nature of the module when loading the session information. So I ended up creating my own module called session.io. It is used like this:

var express = require('express');
var app = express();
var server = require('http').createServer(app);

//Setup cookie and session handlers
//Note: for sessionStore you can use any sessionStore module that has the .load() function
//but I personally use the module 'sessionstore' to handle my sessionStores.
var cookieParser = express.cookieParser('secret');
var sessionStore = require('sessionstore').createSessionStore();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  //...truncate...//
  app.use(cookieParser);
  //make sure to use the same secret as you specified in your cookieParser
  app.use(express.session({secret: 'secret', store: sessionStore}));
  app.use(app.router);
});

app.get('/', function(req, res){
  res.send('<script src="/socket.io/socket.io.js"></script><script>io.connect();</script>Connected');
});

server.listen(app.get('port'), function(){
  console.log('Listening on port ' + app.get('port'));
});

var io = require('socket.io').listen(server);

io.configure(function(){
  //use session.io to get our session data
  io.set('authorization', require('session.io')(cookieParser, sessionStore));
});

io.on('connection', function(socket){
  //we now have access to our session data like so
  var session = socket.handshake.session;
  console.log(session);
});

这篇关于由于连接不使用parseCookie方法了,怎么能使用前preSS我们得到的会话数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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