11 entries · 14-question quiz
Adding Authentication to React Apps
React ↔ Angular reference. React examples use React 19 + TypeScript with React Router's Data Router API (v7; in v6 import from react-router-dom). Angular examples use modern Angular (standalone components, signals, inject(), @if/@for). Builds on the React Router chapter's events app.
Test yourself
14 questions
- 1How Authentication Works
Authentication proves who a user is so the backend can decide whether to grant access to protected resources.
- 2Switching Login and Signup with Query Params
One auth page handles both login and signup, switching modes with a query parameter (
/auth?mode=loginor/auth?mode=signup). - 3Sending Credentials in a Route Action
The auth route's
actionreads the mode from the request URL, sends the credentials to/loginor/signup, and handles validation errors. - 4Storing and Reading the Token
After a successful login, the token is saved in
localStorageso it survives page reloads, and helper functions read it back. - 5Attaching the Token to Requests
Requests to protected endpoints include the token in the
Authorizationheader so the backend can verify it. - 6Updating the UI Based on Auth State
A loader on the root route returns the token, and any component reads it with
useRouteLoaderData('root')to show or hide UI. - 7Logging Out
A logout route with only an
actionclears the token and redirects. The navigation submits to it with<Form action="/logout" method="post">. - 8Protecting Routes
A loader that checks for a token and redirects to the login page if there isn't one. It's React Router's closest equivalent to an Angular route guard.
- 9Token Expiration and Automatic Logout
Tokens expire (often after one hour). The app stores the expiration time and logs the user out automatically when it's reached.
- 10Security Notes and Project Structure
Practical security limits of client-side auth, and where the auth pieces live.
- 11Summary