Module 04 / Project 04 — Authentication¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | — | — | Flashcards | — | — |
Focus¶
JWT tokens, protected routes, password hashing, user registration and login.
Why this project exists¶
Most real APIs need authentication. Users register, log in, and receive a token that proves their identity. This project adds user accounts to the todo API. You will learn how passwords are hashed (never stored in plain text), how JWT tokens work, and how to protect endpoints so only authenticated users can access their own data.
Run¶
Then open http://127.0.0.1:8000/docs and follow this workflow:
- Register a user with
POST /register - Log in with
POST /loginto get a JWT token - Click the "Authorize" button in the
/docsUI and paste the token - Now you can use the protected todo endpoints
Press Ctrl+C to stop the server.
Expected output¶
# Register
curl -X POST http://127.0.0.1:8000/register \
-H "Content-Type: application/json" \
-d '{"username": "alice", "password": "secret123"}'
# Returns: {"id": 1, "username": "alice"}
# Login
curl -X POST http://127.0.0.1:8000/login \
-H "Content-Type: application/json" \
-d '{"username": "alice", "password": "secret123"}'
# Returns: {"access_token": "eyJhbG...", "token_type": "bearer"}
# Create a todo (with token)
curl -X POST http://127.0.0.1:8000/todos \
-H "Content-Type: application/json" \
-H "Authorization: Bearer eyJhbG..." \
-d '{"title": "Learn JWT"}'
# Returns: {"id": 1, "title": "Learn JWT", "completed": false, ...}
# Without token
curl http://127.0.0.1:8000/todos
# Returns: 401 Unauthorized
Alter it¶
- Add token expiration. Make tokens expire after 30 minutes. After expiration, the user must log in again.
- Add a
GET /meendpoint that returns the currently logged-in user's info. - Add a
PUT /me/passwordendpoint that lets users change their password (requires old password and new password).
Break it¶
- Copy a valid JWT token and change one character in the middle. Try to use it. What error do you get?
- Register a user, then try to register the same username again. What happens?
- Try to log in with a correct username but wrong password. What response do you get?
Fix it¶
- The tampered token is rejected because JWTs are cryptographically signed. Any change invalidates the signature. This is working correctly.
- If duplicate registration succeeds, add a check in the register endpoint that queries for existing usernames and raises a 400 error.
- The wrong password should return a 401 Unauthorized. If it does not, check that the password verification function compares the hashed values correctly.
Explain it¶
- Why do we hash passwords instead of storing them directly? What would happen if the database were stolen?
- What information is stored inside a JWT token? Can the server read it without the secret key? Can a client read it?
- What does the
Authorization: Bearer <token>header mean? Why "Bearer"? - What is the difference between authentication (who are you?) and authorization (what can you do?)?
Mastery check¶
You can move on when you can:
- register a user and log in without looking at these instructions,
- explain why passwords are hashed and what a salt does,
- describe the three parts of a JWT token (header, payload, signature),
- add a new protected endpoint from memory.
Related Concepts¶
Next¶
Continue to 05-full-app.