Skip to content
engineering workbench.by StudyPort Academy
Learning path
Intermediate18 min

Protecting routes in practice

Understand the idea

Express refuses the request. React refuses the page. Both look up the same fact: is there a user?

The idea

The Express side is the middleware pattern from Middleware. requireUser reads the user, sends 401 when it is missing, and calls next only after req.user is set. That is on the request. The notes handler then assumes the user exists. The React side is a component, the same kind of function described in JSX and components, used as the element a protected route would render. Routing is where that element is attached to a path. This run renders both branches on the server so you can see them: no user produces Sign in, and ada produces Notes for ada. Hiding the link in a menu does not do this. Anyone who opens the path still hits the component, and anyone who calls the API still hits Express. Both have to refuse.

What changes

The same decision in two places
PlaceMissing userKnown user
Express middleware401, handler does not runnext(), then the notes JSON
React pageSign in markupNotes for that user
A hidden link onlyThe URL still rendersThe URL still renders

The command

Express notes without a user

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

Express notes for ada

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

React page with and without a user

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

The mistake

Check the result

GET /notes is 401. GET /notes?user=ada is 200 and includes Milk and ada. The React run prints Sign in, then Notes for ada.

Guided challenge

Refuse the anonymous request

requireUser currently sets a guest user and always calls next, so GET /notes returns notes. Return 401 when req.query.user is missing, and only then call next.

Refuse the anonymous request

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

EXPECTED RESULT

401
login

Hints 0/2

    Use all hints or make 3 failed checks (0/3).

    Free exploration

    Change this example and run it again. Each run is a new process. Nothing is saved.

    Your variation

    3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Where does Express stop an anonymous notes request?
    02What does the React page render when user is null?

    Ready to call this one understood?

    Mark this lesson complete to earn 150 XP. You can always revisit it.

    Sign in to save progress
    Next lesson