Notif system

This commit is contained in:
Mr Swift
2024-09-28 23:00:10 +03:30
parent 8f63b82236
commit b1f792f493
8 changed files with 130 additions and 0 deletions
@@ -1,5 +1,6 @@
const jwt = require('jsonwebtoken')
const User = require('../../models/User')
const UserGPS = require('../../models/GPS.User')
const authentication = require('../../authentication')
module.exports.isAdmin = async (socket, next) => {
@@ -65,3 +66,23 @@ module.exports.isAgent = async (socket, next) => {
next(new Error(e))
}
}
module.exports.isGPS = async (socket, next) => {
try {
const token = socket.handshake?.auth?.token
if (!token) throw new Error('unauthenticated')
// verifies secret and checks if the token is expired
const decodedToken = await jwt.verify(token.replace(/^Bearer\s/, ''), authentication.secretKey)
if (!decodedToken) throw new Error('unauthorized')
// find user
const user = await UserGPS.findById(decodedToken._id)
if (!user || !user.active) throw new Error('unauthorized')
socket.userId = decodedToken._id.toString()
return next()
} catch (e) {
next(new Error(e))
}
}