jwt key in .env

This commit is contained in:
xfarrow 2023-10-16 15:35:37 +02:00
parent 827f6399e9
commit 2a09c979ba
2 changed files with 7 additions and 4 deletions

View File

@ -2,6 +2,7 @@
# API server settings # API server settings
API_SERVER_PORT = 3000 API_SERVER_PORT = 3000
JWT_SECRET_KEY = jwt-secret # Change this
# Database settings # Database settings
POSTGRES_SERVER = localhost POSTGRES_SERVER = localhost

View File

@ -283,7 +283,9 @@ function generateToken(person_id) {
person_id: person_id person_id: person_id
}; };
const token = jwt.sign(payload, 'your-secret-key', { expiresIn: '1h' }); const token = jwt.sign(payload, process.env.JWT_SECRET_KEY, {
expiresIn: '1h'
});
return token; return token;
} }
@ -292,12 +294,12 @@ function verifyToken(req, res, next) {
const token = req.headers.authorization; const token = req.headers.authorization;
if (!token) { if (!token) {
return res.status(403).send('No token provided'); return res.status(401).send({error : 'No token provided'});
} }
jwt.verify(token, 'your-secret-key', (err, decoded) => { jwt.verify(token, process.env.JWT_SECRET_KEY, (err, decoded) => {
if (err) { if (err) {
return res.status(401).send('Failed to authenticate token'); return res.status(401).send({error : 'Failed to authenticate token'});
} }
// If the token is valid, store the decoded data in the request object // If the token is valid, store the decoded data in the request object