diff --git a/server/functions/api/utils/authenticateToken.js b/server/functions/api/utils/authenticateToken.js new file mode 100644 index 0000000..583f8b7 --- /dev/null +++ b/server/functions/api/utils/authenticateToken.js @@ -0,0 +1,22 @@ +const jwt = require('jsonwebtoken'); + +function authenticateToken(req, res, next) { + const authHeader = req.headers['authorization']; + const token = authHeader && authHeader.split(' ')[1]; + + if (token == null) { + console.log('No token provided'); + return res.sendStatus(401); + } + + jwt.verify(token, process.env.JWT_SECRET || 'fallback_secret_key_for_development', (err, user) => { + if (err) { + console.error('Token verification error:', err); + return res.sendStatus(403); + } + req.user = user; + next(); + }); +} + +module.exports = authenticateToken; \ No newline at end of file