Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update authMiddelware.js #419

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions Backend/Middelware/authMiddelware.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import jwt from 'jsonwebtoken';

export default (req, res, next) => {
const token = req.header('Authorization');
if (!token) return res.status(401).json({ message: 'Access denied. No token provided.' });
const authHeader = req.header('Authorization');

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

const token = authHeader.split(' ')[1]; // Extract the token after "Bearer"

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

try {
const decoded = jwt.verify(token, process.env.SECRET);
Expand All @@ -12,6 +21,6 @@ export default (req, res, next) => {
if (ex.name === 'TokenExpiredError') {
return res.status(401).json({ message: 'Token expired.' });
}
res.status(400).json({ message: 'Invalid token.' });
return res.status(400).json({ message: 'Invalid token.' });
}
};
};