Concept Flow - Why authentication matters
User sends request
Check if user is authenticated
End
This flow shows how a server checks if a user is authenticated before giving access to resources.
app.get('/profile', (req, res) => { if (req.isAuthenticated()) { res.send('User Profile'); } else { res.status(401).send('Please log in'); } });
| Step | Request | Authentication Check | Result | Response Sent |
|---|---|---|---|---|
| 1 | User requests /profile | req.isAuthenticated() called | Returns true | Send 'User Profile' |
| 2 | User requests /profile | req.isAuthenticated() called | Returns false | Send 401 'Please log in' |
| 3 | No more requests | End | End | End |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| req.isAuthenticated() | undefined | true or false | true or false | true or false |
| res.statusCode | undefined | 200 (default) or 401 | 200 or 401 | 200 or 401 |
| res.body | undefined | 'User Profile' or 'Please log in' | 'User Profile' or 'Please log in' | 'User Profile' or 'Please log in' |
Authentication checks if a user is who they say they are. In Express, use req.isAuthenticated() to verify login. If true, allow access; if false, reject or redirect. Protects private data and prevents unauthorized use. Always check before sending sensitive info.