Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why Authorization Differs from Authentication in Express
📖 Scenario: You are building a simple Express server that handles user login and access control. You want to understand the difference between authentication (checking who the user is) and authorization (checking what the user can do).
🎯 Goal: Create a basic Express app that authenticates a user by checking a username and password, then authorizes access to a protected route based on the user's role.
📋 What You'll Learn
Create an object called users with usernames as keys and objects containing password and role as values
Create a variable called loggedInUser initialized to null
Write a function called authenticate that takes username and password and sets loggedInUser if credentials match
Write a function called authorize that takes a role and returns true if loggedInUser has that role
Add an Express route /dashboard that uses authorize to allow access only if the user is an admin
💡 Why This Matters
🌍 Real World
Web apps need to know who users are (authentication) and what they can do (authorization) to protect sensitive data and actions.
💼 Career
Understanding authentication and authorization is essential for backend developers building secure web services with Express.
Progress0 / 4 steps
1
Set up user data
Create an object called users with these exact entries: 'alice' with password 'wonderland' and role 'admin', and 'bob' with password 'builder' and role 'user'.
Express
Hint
Use an object with usernames as keys and objects with password and role as values.
2
Add login state variable
Create a variable called loggedInUser and set it to null to represent no user logged in yet.
Express
Hint
Use let to allow changing the logged in user later.
3
Write authentication function
Write a function called authenticate that takes username and password. If the username exists in users and the password matches, set loggedInUser to the user object; otherwise, set it to null.
Express
Hint
Check if the username exists and password matches, then update loggedInUser.
4
Add authorization and protected route
Write a function called authorize that takes a role and returns true if loggedInUser has that role. Then create an Express app with a route /dashboard that sends 'Welcome admin' if authorized as admin, otherwise sends 'Access denied'.
Express
Hint
Use loggedInUser.role to check authorization. Create an Express route that sends different responses based on authorization.
Practice
(1/5)
1. In Express apps, what is the main difference between authentication and authorization?
easy
A. Authentication checks what the user can access; authorization verifies who they are.
B. Authentication verifies who the user is; authorization checks what they can access.
C. Authentication and authorization both check user identity only.
D. Authorization is done before authentication in Express.
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 B
What status code will be sent if a logged-in user is not an admin?
medium
A. 200
B. 401
C. 403
D. 500
Solution
Step 1: Check authentication condition
The code checks if req.user exists; if not, sends 401 (unauthenticated).
Step 2: Check authorization condition
If user exists but isAdmin is false, sends 403 (forbidden, unauthorized).
Final Answer:
403 -> Option C
Quick Check:
Authenticated but not authorized = 403 [OK]
Hint: 401 = no login, 403 = no permission [OK]
Common Mistakes:
Mixing 401 and 403 status codes
Assuming 200 is sent without admin rights
Ignoring the authorization check
4. This Express middleware aims to protect routes:
function checkAdmin(req, res, next) {
if (!req.user.isAdmin) {
res.status(401).send('Unauthorized');
}
next();
}
What is the bug here?
medium
A. req.user might be undefined causing an error
B. Should send status 403 instead of 401 for authorization failure
C. Missing call to next() inside the if block
D. Middleware should be async
Solution
Step 1: Analyze req.user usage
The code accesses req.user.isAdmin without checking if req.user exists, risking a runtime error.
Step 2: Check other issues
While 403 is better for authorization failure, the main bug is possible crash from undefined req.user.
Final Answer:
req.user might be undefined causing an error -> Option A
Quick Check:
Always check req.user exists before properties [OK]
Hint: Check req.user exists before isAdmin [OK]
Common Mistakes:
Ignoring possible undefined req.user
Confusing 401 and 403 status codes
Not returning after sending response
5. You want to protect an Express route so only authenticated users with role 'editor' or 'admin' can access it. Which middleware logic correctly implements this authorization check?
hard
A. if (req.user && req.user.role === 'admin') { next(); } else { res.status(403).send('Forbidden'); }
B. if (!req.user && (req.user.role === 'editor' || req.user.role === 'admin')) { next(); } else { res.status(401).send('Unauthorized'); }
C. if (req.user.role === 'editor' || req.user.role === 'admin') { next(); } else { res.status(401).send('Unauthorized'); }
D. if (!req.user || (req.user.role !== 'editor' && req.user.role !== 'admin')) { res.status(403).send('Forbidden'); } else { next(); }
Solution
Step 1: Check authentication and authorization together
The middleware must first confirm req.user exists (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 D
Quick Check:
Check user exists AND role allowed for authorization [OK]
Hint: Check user exists AND role matches before next() [OK]