Spaces:
Paused
Paused
Download src/middleware/auth.js from Abhises/backlinks-api: direct link, hf CLI and curl.
- Browser
- Download file 539 Bytes
-
https://huggingface.co/spaces/Abhises/backlinks-api/resolve/main/src/middleware/auth.js
- Command line
-
hf download hf://spaces/Abhises/backlinks-api/src/middleware/auth.js
-
curl -L -o auth.js https://huggingface.co/spaces/Abhises/backlinks-api/resolve/main/src/middleware/auth.js
539 Bytes
| const jwt = require('jsonwebtoken'); | |
| const authMiddleware = (req, res, next) => { | |
| const authHeader = req.headers.authorization; | |
| if (!authHeader || !authHeader.startsWith('Bearer ')) { | |
| return res.status(401).json({ error: 'No token provided' }); | |
| } | |
| const token = authHeader.split(' ')[1]; | |
| try { | |
| const decoded = jwt.verify(token, process.env.JWT_SECRET); | |
| req.user = decoded; | |
| next(); | |
| } catch (err) { | |
| return res.status(401).json({ error: 'Invalid or expired token' }); | |
| } | |
| }; | |
| module.exports = authMiddleware; | |