3.3 KiB
3.3 KiB
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
589fc831f9fc0f352b528e77 | Authentication with Socket.IO | 2 | 使用Socket.IO进行身份验证 |
Description
将“passport.socketio”添加为依赖项,并将其命名为“passportSocketIo”。现在我们只需要告诉Socket.IO使用它并设置选项。确保在现有套接字代码之前添加它,而不是在现有连接侦听器中添加。对于您的服务器,它应如下所示:
io.use(passportSocketIo.authorize({ cookieParser:cookieParser, key:'express.sid', secret:process.env.SESSION_SECRET, store:sessionStore }));您还可以选择将“成功”和“失败”与在客户端尝试连接时身份验证过程完成后调用的函数一起传递。现在可以在套接字对象上以
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