Performance: Why authentication matters
Authentication affects initial page load speed and interaction responsiveness by adding server-side checks and potential redirects.
Jump into concepts and practice - no test required
app.use(async (req, res, next) => { try { const user = await asyncAuthCheck(req.headers.authorization); if (!user) { return res.status(401).send('Unauthorized'); } next(); } catch (err) { next(err); } });
app.use((req, res, next) => {
// Synchronous heavy authentication check
const user = heavyAuthCheck(req.headers.authorization);
if (!user) {
return res.status(401).send('Unauthorized');
} else {
next();
}
});| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous blocking auth | N/A (server-side) | N/A | Delays initial paint | [X] Bad |
| Asynchronous non-blocking auth | N/A (server-side) | N/A | Faster initial paint | [OK] Good |
req.isAuthenticated() is commonly used to check if a user is logged in.app.get('/profile', (req, res) => {
if (!req.isAuthenticated()) {
res.status(401).send('Access denied');
} else {
res.send('User profile');
}
});req.isAuthenticated() is false.function auth(req, res, next) {
if (req.isAuthenticated) {
next();
} else {
res.redirect('/login');
}
}req.isAuthenticated without parentheses, treating it as a property.req.isAuthenticated().