* newlines in Authentication with Socket.IO, test 3 Both my original formatting and glitch's "format this file" button add a newline between "io.use(" and "passport.authorize" ".+" catches one or more of anything that isn't a newline. "[^]+" catches one or more of anything including newlines. This change updates the test accordingly so people who use glitch's formatting don't get left searching for assistance * less permissive match to prevent shenanigans Won't work with comments still. * fix my own silliness
3.5 KiB
3.5 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
589fc831f9fc0f352b528e77 | Authentication with Socket.IO | 2 | 301548 |
Description
Add 'passport.socketio' as a dependency and require it as 'passportSocketIo'. Now we just have to tell Socket.IO to use it and set the options. Be sure this is added before the existing socket code and not in the existing connection listener. For your server it should look as follows:
io.use(passportSocketIo.authorize({
cookieParser: cookieParser,
key: 'express.sid',
secret: process.env.SESSION_SECRET,
store: sessionStore
}));
You can also optionally pass 'success' and 'fail' with a function that will be called after the authentication process completes when a client tries to connect.
The user object is now accessible on your socket object as socket.request.user
. For example, now you can add the following: console.log('user ' + socket.request.user.name + ' connected');
and it will log to the server console who has connected!
Submit your page when you think you've got it right. If you're running into errors, you can check out the project up to this point here.
Instructions
Tests
tests:
- text: passportSocketIo should be a dependency.
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 should be properly required.
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 should be properly setup.
testString: getUserInput => $.get(getUserInput('url')+ '/_api/server.js') .then(data => { assert.match(data, /io\.use\(\s*\w+\.authorize\(/, 'You should register "passport.socketio" as socket.io middleware and provide it correct options'); }, xhr => { throw new Error(xhr.statusText); })
Challenge Seed
Solution
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/