Imagine you have a Flask app where users can see their personal data. Why do you need authentication?
Think about privacy and safety of user information.
Authentication confirms who the user is, so the app shows only their data and keeps others safe.
Consider a Flask route that shows user profile info but has no authentication check. What is the likely result?
Think about what happens if no one checks who is visiting the page.
Without authentication, the app cannot restrict access, so anyone can see any profile.
Given this Flask snippet, what will be printed if a user is not logged in?
from flask import Flask, session app = Flask(__name__) app.secret_key = 'secret' @app.route('/dashboard') def dashboard(): if 'user_id' in session: return 'Welcome user!' else: return 'Please log in first.' # Assume session is empty (no user_id)
Check what happens when 'user_id' is missing in session.
The code checks if 'user_id' is in session. If not, it returns the login prompt.
Choose the code that correctly uses Flask-Login to protect a route so only logged-in users can access it.
Flask-Login provides a decorator named login_required.
Option B uses the correct @login_required decorator from Flask-Login to protect the route.
Examine the code below. Why does it raise a RuntimeError: Working outside of request context?
from flask import Flask, session app = Flask(__name__) app.secret_key = 'secret' user_id = session.get('user_id') @app.route('/') def home(): return f'User ID is {user_id}'
Think about when Flask allows access to session.
Accessing session outside a request context (like at module level) causes this error.