This document explains how authentication tokens specifically Jason Web Tokens (JWTs) are used in the Shopping Assistant application, including how they are generated, what they contain, how they are used, and best practices for secure implementation.
- The token is generated by the FastAPI backend when a user successfully registers or signs in.
- The backend uses a secure library (e.g., PyJWT) to create a JWT, signing it with a secret key.
- The JWT contains encoded user information (claims), such as user ID, username, and an expiration time.
- The FastAPI backend is responsible for generating the token after verifying user credentials.
- The backend creates and returns the JWT to the frontend (web app) upon successful authentication.
A typical JWT includes:
sub(subject): the user’s unique IDusernameoremailexp: expiration timestamp- Optional: user roles, permissions, or other claims
Example JWT payload:
{
"sub": "12345",
"username": "alex",
"exp": 1715700000
}- After sign-in/registration, the frontend stores the JWT (preferably in memory or a secure HTTP-only cookie).
- For each subsequent API request, the frontend includes the token in the HTTP Authorization header:
Authorization: Bearer <JWT> - The backend extracts and verifies the token on every request, authenticating the user and authorizing actions.
- User registers or signs in.
- Backend verifies credentials and generates a JWT (using a strong secret, short expiration, and secure claims).
- Frontend receives and stores the JWT (preferably in memory or a secure cookie).
- Frontend sends the JWT in the Authorization header for every authenticated API request.
- Backend validates the JWT on each request, extracts user info, and processes the request.
- On logout, the frontend deletes the token (and, if using cookies, the backend can also blacklist tokens if needed).
- Use HTTPS for all communication.
- Use short-lived tokens and refresh tokens if needed.
- Store tokens securely (avoid localStorage for sensitive apps; prefer HTTP-only cookies or in-memory storage).
- Never expose your JWT secret.
- Validate the token signature and expiration on every request.
Summary for this app:
- FastAPI backend generates and signs the JWT.
- JWT contains user identity and session info.
- Frontend sends the JWT in the Authorization header for each request.
- Backend authenticates and authorizes using the token for every operation (orders, queries, etc.).