Notif system
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user