WS finial test
This commit is contained in:
@@ -69,16 +69,26 @@ module.exports.isAgent = async (socket, next) => {
|
||||
|
||||
module.exports.isGPS = async (socket, next) => {
|
||||
try {
|
||||
console.log('1');
|
||||
|
||||
const token = socket.handshake?.auth?.token
|
||||
if (!token) throw new Error('unauthenticated')
|
||||
|
||||
if (!token) {
|
||||
console.log('unauthorized 1');
|
||||
throw new Error('unauthorized')
|
||||
}
|
||||
// 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')
|
||||
if (!decodedToken) {
|
||||
console.log('unauthorized 2');
|
||||
throw new Error('unauthorized')
|
||||
}
|
||||
|
||||
// find user
|
||||
const user = await UserGPS.findById(decodedToken._id)
|
||||
if (!user || !user.active) throw new Error('unauthorized')
|
||||
if (!user || !user.active) {
|
||||
console.log('unauthorized 3');
|
||||
throw new Error('unauthorized')
|
||||
}
|
||||
|
||||
socket.userId = decodedToken._id.toString()
|
||||
return next()
|
||||
|
||||
Reference in New Issue
Block a user