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 authentication matters
📖 Scenario: You are building a simple Express server for a small online store. You want to protect a special route that shows sensitive user information. To do this, you will add a basic authentication check.
🎯 Goal: Build an Express server with a protected route /profile that only allows access if a correct password is provided in the request headers.
📋 What You'll Learn
Create an Express app variable called app
Set a password variable called correctPassword with the value secret123
Add a middleware function called checkAuth that checks if the request header password matches correctPassword
Use the checkAuth middleware on the /profile route to protect it
💡 Why This Matters
🌍 Real World
Authentication is essential to protect user data and private routes in web applications. This project shows a simple way to check credentials before allowing access.
💼 Career
Understanding middleware and authentication in Express is a key skill for backend web development jobs.
Progress0 / 4 steps
1
Set up Express app
Create a variable called express by requiring the 'express' module. Then create a variable called app by calling express().
Express
Hint
Use require('express') to import Express and then call it to create the app.
2
Add password variable
Create a variable called correctPassword and set it to the string 'secret123'.
Express
Hint
Just create a simple string variable with the password.
3
Create authentication middleware
Write a middleware function called checkAuth that takes req, res, and next as parameters. Inside it, check if req.headers.password equals correctPassword. If yes, call next(). Otherwise, respond with status 401 and message 'Unauthorized'.
Express
Hint
Middleware functions have three parameters and must call next() to continue.
4
Protect the /profile route
Add a GET route /profile to app that uses the checkAuth middleware. The route handler should send the text 'User profile data'.
Express
Hint
Use app.get with the middleware as the second argument, then the handler.
Practice
(1/5)
1. Why is authentication important in an Express app?
easy
A. It speeds up the app's performance.
B. It confirms the user's identity to protect data and features.
C. It changes the app's color scheme.
D. It automatically fixes bugs in the code.
Solution
Step 1: Understand the purpose of authentication
Authentication is used to confirm who a user is before allowing access.
Step 2: Connect authentication to app protection
By confirming identity, the app protects sensitive data and features from unauthorized users.
Final Answer:
It confirms the user's identity to protect data and features. -> Option B
Quick Check:
Authentication = Confirm identity [OK]
Hint: Authentication means checking who the user is [OK]
Common Mistakes:
Thinking authentication improves speed
Confusing authentication with UI changes
Believing it fixes code bugs automatically
2. Which Express code snippet correctly checks if a user is authenticated before accessing a route?
The code sends 'Access denied' with status 401 if req.isAuthenticated() is false.
Step 2: Determine output for unauthenticated user
Since the user is unauthenticated, the condition is true and 'Access denied' is sent.
Final Answer:
Access denied -> Option A
Quick Check:
Unauthenticated user gets 'Access denied' [OK]
Hint: Check if condition sends 'Access denied' for unauthenticated [OK]
Common Mistakes:
Assuming unauthenticated users see profile
Expecting a redirect instead of 401 status
Confusing 404 with access denial
4. Identify the error in this Express authentication middleware:
function auth(req, res, next) {
if (req.isAuthenticated) {
next();
} else {
res.redirect('/login');
}
}
medium
A. res.redirect should be res.sendRedirect
B. next() should be res.next()
C. Middleware should return a value
D. Missing parentheses in req.isAuthenticated call
Solution
Step 1: Check how req.isAuthenticated is used
The code uses req.isAuthenticated without parentheses, treating it as a property.
Step 2: Correct usage of req.isAuthenticated()
It is a function and must be called with parentheses: req.isAuthenticated().
Final Answer:
Missing parentheses in req.isAuthenticated call -> Option D
Quick Check:
Call req.isAuthenticated() as a function [OK]
Hint: Remember req.isAuthenticated() needs () to call [OK]
Common Mistakes:
Using req.isAuthenticated without ()
Confusing next() with res.next()
Using wrong redirect method name
5. You want to protect multiple routes in your Express app so only authenticated users can access them. Which approach best applies authentication efficiently?
hard
A. Create an authentication middleware and apply it to all protected routes.
B. Add the authentication check inside each route handler separately.
C. Check authentication only on the homepage route.
D. Use client-side JavaScript to hide protected routes.
Solution
Step 1: Understand route protection needs
Multiple routes require the same authentication check to avoid repeating code.
Step 2: Use middleware for efficient authentication
Middleware can be applied to many routes at once, centralizing the check and improving maintainability.
Final Answer:
Create an authentication middleware and apply it to all protected routes. -> Option A
Quick Check:
Middleware centralizes authentication checks [OK]
Hint: Use middleware to protect many routes at once [OK]