-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddlewares.js
37 lines (31 loc) · 1.22 KB
/
middlewares.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const Room = require('./models/Room')
const User = require('./models/User')
const authenticateAdmin = (req, res, next) => {
const {roomId, roomKey} = req.cookies
if(!roomId || !roomKey) return res.send("try creating a new room...")
Room.findById(roomId, (err, foundRoom) => {
if(err) return res.send('Interal Server Error')
if(!foundRoom) return res.redirect('/')
if(roomKey != foundRoom.roomKey) return res.redirect('/')
next()
})
}
const authenticateUser = (req, res, next) => {
const {roomId, userId, username, userSecret} = req.cookies
if(!roomId || !userId || !username || !userSecret) return res.send("please join again...")
User.findById(userId, (err, foundUser) => {
if (err) return res.send('Internal Server Error')
if(!foundUser || foundUser.roomId != roomId || foundUser.userSecret !== userSecret )
return res.send('Please rejoin the room')
next()
})
}
const roomIsPlaying = (req, res, next) => {
const {roomId} = req.cookies
Room.findById(roomId, (err, foundRoom) => {
if(foundRoom.isPlaying) {
return next()
}
})
}
module.exports = {authenticateAdmin, authenticateUser, roomIsPlaying}