Performance: User login flow
This affects the server response time and perceived page load speed during user authentication.
Jump into concepts and practice - no test required
app.post('/login', async (req, res) => { const user = await db.findUserAsync(req.body.username); if (!user || user.password !== req.body.password) { return res.status(401).send('Unauthorized'); } req.session.user = user; res.redirect('/dashboard'); });
app.post('/login', (req, res) => { const user = db.findUser(req.body.username); if (!user) return res.status(401).send('Unauthorized'); if (user.password !== req.body.password) return res.status(401).send('Unauthorized'); req.session.user = user; res.redirect('/dashboard'); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous DB and password check | Minimal | 0 | 0 | [X] Bad |
| Asynchronous DB and password check | Minimal | 0 | 0 | [OK] Good |
app.post('/login', (req, res) => {
const { username, password } = req.body;
if(username === 'user' && password === 'pass') {
req.session.user = username;
res.send('Login successful');
} else {
res.status(401).send('Invalid credentials');
}
});app.post('/login', (req, res) => {
const { username, password } = req.body;
if(username == 'admin' && password == '1234') {
res.session.user = username;
res.send('Welcome admin');
} else {
res.send('Access denied');
}
});1. Use express-session middleware 2. On successful login, save username in req.session 3. On other routes, check if req.session.user exists 4. If exists, allow access; else redirect to login