-
-
Save camperbot/1414cc9433044e306dd7fd0caa1c6254 to your computer and use it in GitHub Desktop.
| 'use strict'; | |
| require('dotenv').config(); | |
| const express = require('express'); | |
| const myDB = require('./connection'); | |
| const fccTesting = require('./freeCodeCamp/fcctesting.js'); | |
| const session = require('express-session'); | |
| const passport = require('passport'); | |
| const routes = require('./routes'); | |
| const auth = require('./auth.js'); | |
| const app = express(); | |
| const http = require('http').createServer(app); | |
| const io = require('socket.io')(http); | |
| const passportSocketIo = require('passport.socketio'); | |
| const cookieParser = require('cookie-parser'); | |
| const MongoStore = require('connect-mongo')(session); | |
| const URI = process.env.MONGO_URI; | |
| const store = new MongoStore({ url: URI }); | |
| app.set('view engine', 'pug'); | |
| fccTesting(app); //For FCC testing purposes | |
| app.use('/public', express.static(process.cwd() + '/public')); | |
| app.use(express.json()); | |
| app.use(express.urlencoded({ extended: true })); | |
| app.use(session({ | |
| secret: process.env.SESSION_SECRET, | |
| resave: true, | |
| saveUninitialized: true, | |
| cookie: { secure: false }, | |
| key: 'express.sid', | |
| store: store | |
| })); | |
| app.use(passport.initialize()); | |
| app.use(passport.session()); | |
| io.use( | |
| passportSocketIo.authorize({ | |
| cookieParser: cookieParser, | |
| key: 'express.sid', | |
| secret: process.env.SESSION_SECRET, | |
| store: store, | |
| success: onAuthorizeSuccess, | |
| fail: onAuthorizeFail | |
| }) | |
| ); | |
| myDB(async (client) => { | |
| const myDataBase = await client.db('database').collection('users'); | |
| routes(app, myDataBase); | |
| auth(app, myDataBase); | |
| let currentUsers = 0; | |
| io.on('connection', (socket) => { | |
| ++currentUsers; | |
| io.emit('user count', currentUsers); | |
| console.log('user ' + socket.request.user.username + ' connected'); | |
| socket.on('disconnect', () => { | |
| console.log('A user has disconnected'); | |
| --currentUsers; | |
| io.emit('user count', currentUsers); | |
| }); | |
| }); | |
| }).catch((e) => { | |
| app.route('/').get((req, res) => { | |
| res.render('pug', { title: e, message: 'Unable to login' }); | |
| }); | |
| }); | |
| function onAuthorizeSuccess(data, accept) { | |
| console.log('successful connection to socket.io'); | |
| accept(null, true); | |
| } | |
| function onAuthorizeFail(data, message, error, accept) { | |
| if (error) throw new Error(message); | |
| console.log('failed connection to socket.io:', message); | |
| accept(null, false); | |
| } | |
| http.listen(process.env.PORT || 3000, () => { | |
| console.log('Listening on port ' + process.env.PORT); | |
| }); |
how to pass the tests since this dependency cannot be installed?
Make sure you use the versions specified. i.e. passport.socketio@~3.7.0, connect-mongo@~3.2.0, and cookie-parser@~1.4.5
till they update the material and tests.
I just tried a different dependency called 'connect-mongodb-session' and it worked. You can view it here: https://www.npmjs.com/package/connect-mongodb-session
worked for me. Just remember to access user name like this -> socket.request.user.username instead of -> socket.request.user.name.
Use this process if you want to still use connect-mongo: https://stackoverflow.com/questions/66654037/mongo-connect-error-with-mongo-connectsession. And use this process to replace passport-socket.io since it is deprecated: https://github.com/jfromaniello/passport.socketio/issues/148.
Still not passed the test.
Please recommend.
Below is my server.js.
'use strict';
require('dotenv').config();
const express = require('express');
const myDB = require('./connection');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const session = require('express-session');
const passport = require('passport');
const routes = require('./routes');
const auth = require('./auth.js');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const passportSocketIo = require('passport.socketio');
const cookieParser = require('cookie-parser');
var MongoStore = require('connect-mongodb-session')(session);
const URI = process.env.MONGO_URI;
const store = new MongoStore({ url: URI });
app.set('view engine', 'pug');
fccTesting(app); //For FCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
cookie: { secure: false },
key: 'express.sid',
store: store
}));
app.use(passport.initialize());
app.use(passport.session());
io.use(
passportSocketIo.authorize({
cookieParser: cookieParser,
key: 'express.sid',
secret: process.env.SESSION_SECRET,
store: store,
success: onAuthorizeSuccess,
fail: onAuthorizeFail
})
);
myDB(async (client) => {
const myDataBase = await client.db('database').collection('users');
routes(app, myDataBase);
auth(app, myDataBase);
let currentUsers = 0;
io.on('connection', (socket) => {
++currentUsers;
io.emit('user count', currentUsers);
console.log('user ' + socket.request.user.username + ' connected');
socket.on('disconnect', () => {
console.log('A user has disconnected');
--currentUsers;
io.emit('user count', currentUsers);
});
});
}).catch((e) => {
app.route('/').get((req, res) => {
res.render('pug', { title: e, message: 'Unable to login' });
});
});
function onAuthorizeSuccess(data, accept) {
console.log('successful connection to socket.io');
accept(null, true);
}
function onAuthorizeFail(data, message, error, accept) {
if (error) throw new Error(message);
console.log('failed connection to socket.io:', message);
accept(null, false);
}
http.listen(process.env.PORT || 3000, () => {
console.log('Listening on port ' + process.env.PORT);
});
Try using connect-mongo using the method I linked above: https://stackoverflow.com/questions/66654037/mongo-connect-error-with-mongo-connectsession. You need to use this syntax for declaration: const MongoDBStore = require('connect-mongo'); const URI = process.env.MONGO_URI; const store = MongoDBStore.create({ mongoUrl: URI });
I am not sure if passport-socket.io will work for you as it did not for my set up so I suggest using this method to replace it: https://github.com/jfromaniello/passport.socketio/issues/148. Your tests may not even pass but if you run it, it should work perfectly fine if you follow those links.
Thank you.
passport.socketio still works for me