Authentication vs. Authorization
Before building, clarify the two: Authentication (AuthN) is proving who you are (e.g., login). Authorization (AuthZ) is proving what you are allowed to do (e.g., admin vs. user). In a modern architecture, your Python backend verifies the identity and then issues a credential that the client (like a Flutter app) uses for subsequent requests.
- Session-Based Authentication
Traditional web apps use Sessions. The server creates a session record in the database/memory and sends a 'Session ID' to the browser via a cookie. This is the default in Django and Flask-Login.
- Pros: Easy to revoke, built-in CSRF protection.
- Cons: Harder to scale horizontally (requires shared session storage like Redis), not ideal for mobile apps.
- Token-Based Authentication (JWT)
JWTs are the standard for modern APIs. The server signs a JSON object containing user data and sends it to the client. The client sends this token in the Authorization: Bearer <token> header for every request.
- OAuth2 and OpenID Connect (OIDC)
If you want to allow 'Login with Google' or 'Login with GitHub', you use OAuth2. Python libraries like Authlib or PyOIDC handle the complex handshake between your app and the identity provider.
Authentication Methods Comparison
| Method | Storage | State | Use Case |
|---|---|---|---|
| Basic Auth | Headers (Base64) | Stateless | Simple internal scripts |
| Sessions | Server DB / Cookies | Stateful | Traditional Web Apps |
| JWT / Tokens | Client-side / Header | Stateless | Mobile Apps & SPAs |
| OAuth2 | External Provider | Delegated | Third-party login (SSO) |