From b66d2773b2e23695502d2579e084f37710187202 Mon Sep 17 00:00:00 2001 From: singharaj usai Date: Mon, 30 Sep 2024 10:29:52 -0400 Subject: [PATCH] authentication token for server folders organization later --- .../functions/api/utils/authenticateToken.js | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 server/functions/api/utils/authenticateToken.js 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