Spaces:
Sleeping
Sleeping
Download backend/src/middleware/optionalAuth.js from monumkp89/pixelcraft: direct link, hf CLI and curl.
- Browser
- Download file 429 Bytes
-
https://huggingface.co/spaces/monumkp89/pixelcraft/resolve/main/backend/src/middleware/optionalAuth.js
- Command line
-
hf download hf://spaces/monumkp89/pixelcraft/backend/src/middleware/optionalAuth.js
-
curl -L -o optionalAuth.js https://huggingface.co/spaces/monumkp89/pixelcraft/resolve/main/backend/src/middleware/optionalAuth.js
429 Bytes
| const jwt = require('jsonwebtoken'); | |
| const optionalAuth = (req, res, next) => { | |
| const token = req.headers.authorization?.split(' ')[1]; | |
| if (!token) { | |
| next(); | |
| return; | |
| } | |
| try { | |
| const decoded = jwt.verify(token, process.env.JWT_SECRET); | |
| req.userId = decoded.userId; | |
| } catch (error) { | |
| // Ignore invalid optional auth and continue as a public request. | |
| } | |
| next(); | |
| }; | |
| module.exports = optionalAuth; | |