我正在尝试使用socket.io构建一个使用jwt进行身份验证的聊天应用程序,并且每次连接均会检查jwt,但是一旦我从一台设备登录,然后使用jwt连接,然后注销并再次签名作为其他用户使用socket.io使用先前用户的jwt。
如果用户具有有效的jwt,则这是用于验证用户身份的代码,则授予连接,否则将被拒绝
io.use(function(socket, next){
if(socket.handshake.query && socket.handshake.query.token){
console.log(socket.handshake.query.token);
const token = socket.handshake.query.token.split(" ");
const actualToken = token[1];
console.log(actualToken);
jwt.verify(actualToken,config.secret,function(err,decoded){
if(err) return next(new Error("Authentication error"));
socket.user = decoded.user;
next();
});
}
else{
next(new Error("Authentication error"));
}
})