Skip to content

Commit

Permalink
.env
Browse files Browse the repository at this point in the history
  • Loading branch information
Nmesoma01 committed Jul 24, 2024
1 parent 7878d9a commit 521bef6
Show file tree
Hide file tree
Showing 4 changed files with 856 additions and 6,297 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/node_modules
/node_modules

.env
15 changes: 10 additions & 5 deletions middleware/auth.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
const jwt = require('jsonwebtoken');

const authMiddleware = (req, res, next) => {
const token = req.header('Authorization').replace('Bearer ', '');
// Retrieve the token from the Authorization header
const token = req.header('Authorization')?.replace('Bearer ', '');

// Check if token is provided
if (!token) {
return res.status(401).send('Access denied');
return res.status(401).json({ message: 'Access denied: No token provided' });
}

try {
const decoded = jwt.verify(token, 'your_jwt_secret');
// Verify the token using the secret stored in an environment variable
const decoded = jwt.verify(token, process.env.JWT_SECRET);
req.user = decoded;
next();
} catch (error) {
res.status(400).send('Invalid token');
} catch {
// Handle invalid token error
res.status(400).json({ message: 'Invalid token' });
}
};

Expand Down
Loading

0 comments on commit 521bef6

Please sign in to comment.