Discover why knowing who you are isn't enough to keep your app safe!
Why authorization differs from authentication in Express - The Real Reasons
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you build a website where users log in and access different pages. You check their username and password manually every time they visit a page, then try to remember who can see what.
Doing both login checks and permission checks by hand is confusing and slow. You might let someone see things they shouldn't or block them by mistake. It's hard to keep track of who is who and what they can do.
Authentication confirms who the user is, while authorization decides what they can do. Separating these makes your code clearer and safer. Express libraries help handle each step properly without mixing them up.
if (username === 'admin' && password === '123') { if (page === 'admin') { showPage(); } else { denyAccess(); } }
authenticateUser(req, res, next); authorizeUser(req, res, next);
This separation lets you build secure apps where users log in once and get the right access everywhere, without confusion or mistakes.
Think of a company website where employees log in (authentication) but only managers can see salary info (authorization). This keeps sensitive data safe.
Authentication checks who you are.
Authorization checks what you can do.
Keeping them separate makes apps safer and easier to manage.
Practice
authentication and authorization?Solution
Step 1: Understand authentication purpose
Authentication confirms the user's identity, like logging in.Step 2: Understand authorization purpose
Authorization decides what resources or actions the authenticated user can access.Final Answer:
Authentication verifies who the user is; authorization checks what they can access. -> Option BQuick Check:
Authentication = identity, Authorization = permissions [OK]
- Confusing authentication with authorization
- Thinking both check the same thing
- Assuming authorization happens before authentication
authentication?Solution
Step 1: Identify authentication middleware
Passport.js is a popular Express middleware for handling authentication.Step 2: Check other options
express.static serves files, express.json parses JSON, cors handles cross-origin requests, none handle authentication.Final Answer:
passport.authenticate() -> Option AQuick Check:
passport.authenticate() = authentication middleware [OK]
- Choosing express.static for authentication
- Confusing cors with authentication
- Not knowing passport middleware
app.get('/dashboard', (req, res) => {
if (!req.user) {
return res.status(401).send('Not authenticated');
}
if (!req.user.isAdmin) {
return res.status(403).send('Not authorized');
}
res.send('Welcome Admin');
});What status code will be sent if a logged-in user is not an admin?
Solution
Step 1: Check authentication condition
The code checks ifreq.userexists; if not, sends 401 (unauthenticated).Step 2: Check authorization condition
If user exists butisAdminis false, sends 403 (forbidden, unauthorized).Final Answer:
403 -> Option CQuick Check:
Authenticated but not authorized = 403 [OK]
- Mixing 401 and 403 status codes
- Assuming 200 is sent without admin rights
- Ignoring the authorization check
function checkAdmin(req, res, next) {
if (!req.user.isAdmin) {
res.status(401).send('Unauthorized');
}
next();
}What is the bug here?
Solution
Step 1: Analyze req.user usage
The code accessesreq.user.isAdminwithout checking ifreq.userexists, risking a runtime error.Step 2: Check other issues
While 403 is better for authorization failure, the main bug is possible crash from undefinedreq.user.Final Answer:
req.user might be undefined causing an error -> Option AQuick Check:
Always check req.user exists before properties [OK]
- Ignoring possible undefined req.user
- Confusing 401 and 403 status codes
- Not returning after sending response
Solution
Step 1: Check authentication and authorization together
The middleware must first confirmreq.userexists (authenticated), then check if role is 'editor' or 'admin'.Step 2: Analyze each option
if (!req.user || (req.user.role !== 'editor' && req.user.role !== 'admin')) { res.status(403).send('Forbidden'); } else { next(); } correctly denies access if no user or role not allowed, sending 403 Forbidden. Others have logic errors or wrong status codes.Final Answer:
if (!req.user || (req.user.role !== 'editor' && req.user.role !== 'admin')) { res.status(403).send('Forbidden'); } else { next(); } -> Option DQuick Check:
Check user exists AND role allowed for authorization [OK]
- Not checking if user is authenticated first
- Using wrong status codes (401 vs 403)
- Incorrect logical operators in role check
