freeCodeCamp/curriculum/challenges/chinese/06-information-security-and.../advanced-node-and-express/authentication-with-socket....

3.3 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
589fc831f9fc0f352b528e77 Authentication with Socket.IO 2 使用Socket.IO进行身份验证

Description

提醒一下,这个项目是基于Glitch的以下入门项目构建的,或者是从GitHub克隆的。目前您无法确定谁连接到您的Web套接字。虽然'req.user'对用户对象进行了容器处理但只有当您的用户与Web服务器进行交互并且使用Web套接字时您才没有req请求因此没有用户数据。解决知道谁连接到您的Web套接字的问题的一种方法是解析和解码包含护照会话的cookie然后对其进行反序列化以获取用户对象。幸运的是NPM上有一个包只是为了将一次复杂的任务变成简单的事情
将“passport.socketio”添加为依赖项并将其命名为“passportSocketIo”。现在我们只需要告诉Socket.IO使用它并设置选项。确保在现有套接字代码之前添加它而不是在现有连接侦听器中添加。对于您的服务器它应如下所示
 io.usepassportSocketIo.authorize{
  cookieParsercookieParser
  key'express.sid'
  secretprocess.env.SESSION_SECRET
  storesessionStore
}; 
您还可以选择将“成功”和“失败”与在客户端尝试连接时身份验证过程完成后调用的函数一起传递。现在可以在套接字对象上以socket.request.user访问用户对象。例如,现在您可以添加以下内容: console.log('user ' + socket.request.user.name + ' connected');它将登录已连接的服务器控制台!当您认为自己已经做对时,请提交您的页面。如果您遇到错误,可以在此处查看项目。

Instructions

Tests

tests:
  - text: passportSocketIo是一个依赖项
    testString: 'getUserInput => $.get(getUserInput("url")+ "/_api/package.json") .then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, "passport.socketio", "Your project should list "passport.socketio" as a dependency"); }, xhr => { throw new Error(xhr.statusText); })'
  - text: passportSocketIo是正确需要的
    testString: 'getUserInput => $.get(getUserInput("url")+ "/_api/server.js").then(data => { assert.match(data, /require\(([""])passport\.socketio\1\)/gi, "You should correctly require and instantiate "passport.socketio"");}, xhr => { throw new Error(xhr.statusText); })'
  - text: passportSocketIo已正确设置
    testString: 'getUserInput => $.get(getUserInput("url")+ "/_api/server.js") .then(data => { assert.match(data, /io\.use\(.+\.authorize\(/gi, "You should register "passport.socketio" as socket.io middleware and provide it correct options"); }, xhr => { throw new Error(xhr.statusText); })'

Challenge Seed

Solution

// solution required