kickstart-express
Version:
A powerful CLI tool to quickly scaffold Express.js projects with modern tooling and best practices
34 lines (30 loc) • 1.06 kB
JavaScript
import jwt from 'jsonwebtoken';
/**
* JWT Authentication Middleware
* Validates JWT tokens and attaches user information to the request
*/
export const authMiddleware = (req, res, next) => {
try {
// Get token from Authorization header
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(401).json({
error: 'Access denied. No token provided or invalid format.'
});
}
const token = authHeader.substring(7); // Remove 'Bearer ' prefix
// Verify token
const decoded = jwt.verify(token, process.env.JWT_SECRET);
// Attach user information to request
req.user = decoded;
next();
} catch (error) {
if (error instanceof jwt.TokenExpiredError) {
return res.status(401).json({ error: 'Token has expired.' });
}
if (error instanceof jwt.JsonWebTokenError) {
return res.status(401).json({ error: 'Invalid token.' });
}
return res.status(500).json({ error: 'Token verification failed.' });
}
};