Skip to content

Commit

Permalink
authentication token for server folders organization later
Browse files Browse the repository at this point in the history
  • Loading branch information
singharaj-usai committed Sep 30, 2024
1 parent 767420e commit b66d277
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions server/functions/api/utils/authenticateToken.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit b66d277

Please sign in to comment.