Download server/middleware/socket-auth.js from eznd/bao: direct link, hf CLI and curl.
- Browser
- Download file 646 Bytes
-
https://huggingface.co/spaces/eznd/bao/resolve/main/server/middleware/socket-auth.js
- Command line
-
hf download hf://spaces/eznd/bao/server/middleware/socket-auth.js
-
curl -L -o socket-auth.js https://huggingface.co/spaces/eznd/bao/resolve/main/server/middleware/socket-auth.js
646 Bytes
| const jwt = require('jsonwebtoken'); | |
| function authenticateSocket(socket, next) { | |
| // The token is sent in the 'auth' object during connection | |
| const token = socket.handshake.auth.token; | |
| if (!token) { | |
| return next(new Error('Authentication error: No token provided.')); | |
| } | |
| jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => { | |
| if (err) { | |
| return next(new Error('Authentication error: Invalid token.')); | |
| } | |
| // Attach the decoded user payload to the socket object | |
| socket.user = decoded; | |
| next(); | |
| }); | |
| } | |
| module.exports = authenticateSocket; | |