-
-
Save camperbot/1414cc9433044e306dd7fd0caa1c6254 to your computer and use it in GitHub Desktop.
Advanced Node and Express - Authentication with Socket.IO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.