WS finial test

This commit is contained in:
Mr Swift
2024-09-30 12:55:01 +03:30
parent b1f792f493
commit 9fa38c8adb
6 changed files with 209 additions and 63 deletions
@@ -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()