Concept Flow - Why authentication matters
User sends request
Check if user is logged in
Show [User logs in
Grant access
Show content
This flow shows how a web app checks if a user is logged in before giving access to protected content.
from flask import Flask, session, redirect app = Flask(__name__) @app.route('/dashboard') def dashboard(): if 'user' in session: return 'Welcome!' else: return redirect('/login')
| Step | Request URL | Session State | Condition ('user' in session) | Action | Output |
|---|---|---|---|---|---|
| 1 | /dashboard | session = {} | False | Redirect to /login | Redirect response |
| 2 | /login | session = {} | N/A | Show login form | Login page content |
| 3 | POST /login | session = {} | N/A | Set session['user'] | User logged in |
| 4 | /dashboard | session = {'user': 'alice'} | True | Show dashboard content | Welcome! |
| 5 | /dashboard | session = {'user': 'alice'} | True | Show dashboard content | Welcome! |
| Variable | Start | After Step 1 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|
| session | {} | {} | {'user': 'alice'} | {'user': 'alice'} | {'user': 'alice'} |
| condition ('user' in session) | False | False | False | True | True |
Authentication flow in Flask: - Check if 'user' key exists in session - If yes, allow access to protected pages - If no, redirect to login page - After login, set 'user' in session - Protects content from unauthorized access