Performance: JWT token verification middleware
MEDIUM IMPACT
This affects the server response time and user interaction speed by adding token verification before processing requests.
import { promisify } from 'util'; const verifyAsync = promisify(jwt.verify); app.use(async (req, res, next) => { try { const token = req.headers.authorization?.split(' ')[1]; if (!token) return res.status(401).send('No token'); req.user = await verifyAsync(token, 'secret'); next(); } catch { res.status(403).send('Invalid token'); } });
app.use((req, res, next) => {
const token = req.headers.authorization?.split(' ')[1];
if (!token) return res.status(401).send('No token');
jwt.verify(token, 'secret', (err, decoded) => {
if (err) return res.status(403).send('Invalid token');
req.user = decoded;
next();
});
});| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous JWT verification middleware | 0 (server-side) | 0 | 0 | [X] Bad |
| Asynchronous JWT verification middleware | 0 (server-side) | 0 | 0 | [OK] Good |