Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js+;socket.io+;蒙戈授权_Node.js_Mongodb_Authentication_Cookies_Socket.io - Fatal编程技术网

Node.js+;socket.io+;蒙戈授权

Node.js+;socket.io+;蒙戈授权,node.js,mongodb,authentication,cookies,socket.io,Node.js,Mongodb,Authentication,Cookies,Socket.io,在我的网站上,用户需要使用数据库帐户登录。我的问题是,只有当数据库登录通过并在cookie中创建我的变量时,如何对socket.io进行身份验证 Express with auth函数从这里开始使用sessionID,也许我可以防止在用户登录数据库时将sessionID指定给用户?使用: 用数据库查找替换findDatabyIP 通过socket.handshake.sessionID,您在握手数据上设置的任何属性都应该在“连接”处理程序中可用 完成后调用callback,并显示错误消息(如果

在我的网站上,用户需要使用数据库帐户登录。我的问题是,只有当数据库登录通过并在cookie中创建我的变量时,如何对socket.io进行身份验证

Express with auth函数从这里开始使用sessionID,也许我可以防止在用户登录数据库时将sessionID指定给用户?

使用:

  • 用数据库查找替换findDatabyIP
  • 通过socket.handshake.sessionID,您在握手数据上设置的任何属性都应该在“连接”处理程序中可用
  • 完成后调用callback,并显示错误消息(如果适用),如果连接已授权,则为true;如果未授权,则为false
  • 解析和读取会话的cookie可能很困难。也许会有帮助
io.set('authorization', function (handshakeData, callback) {
  // findDatabyip is an async example function
  findDatabyIP(handshakeData.address.address, function (err, data) {
    if (err) return callback(err);

    if (data.authorized) {
      handshakeData.foo = 'bar';
      for(var prop in data) handshakeData[prop] = data[prop];
      callback(null, true);
    } else {
      callback(null, false);
    }
  }) 
});