INNER CODE UNIT · JavaScript
authSocket
ihtasham42/social-media-app · socketServer.js:4
const authSocket = (socket, next) => {
let token = socket.handshake.auth.token;
if (token) {
try {
const decoded = jwt.verify(token, process.env.TOKEN_KEY);
socket.decoded = decoded;
next();
} catch (err) {
next(new Error("Authentication error"));
}
} else {
next(new Error("Authentication error"));
}
};
const socketServer = (socket) => {
const userId = socket.decoded.userId;