Consider a Flask app using Flask-Login. After calling logout_user(), what is the state of current_user.is_authenticated?
from flask_login import logout_user, current_user logout_user() print(current_user.is_authenticated)
Think about what logout_user() does to the user session.
Calling logout_user() removes the user session, so current_user.is_authenticated becomes False.
Choose the correct Flask route code that logs out the user and redirects to the home page.
from flask import redirect, url_for from flask_login import logout_user @app.route('/logout') def logout(): # logout code here return redirect(url_for('home'))
Remember to call the function and use url_for for redirect.
Option D correctly calls logout_user() as a function and uses url_for to redirect.
Given this Flask logout route, why does it raise an error?
from flask import redirect, url_for from flask_login import logout_user @app.route('/logout') def logout(): logout_user return redirect(url_for('home'))
Check how functions are called in Python.
Without parentheses, logout_user is a function object, not called. This causes a TypeError when Flask tries to execute the route.
In Flask with Flask-Login, after calling logout_user(), what happens to the session data related to the user?
from flask import session from flask_login import logout_user logout_user() print('_user_id' in session)
Think about what Flask-Login stores in the session for logged-in users.
Flask-Login stores the user ID in the session under the key '_user_id'. logout_user() removes this key, so '_user_id' in session is False.
In Flask-Login, why should the logout route usually be decorated with @login_required?
@app.route('/logout') @login_required def logout(): logout_user() return redirect(url_for('home'))
Consider what happens if a user who is not logged in tries to logout.
Protecting logout with @login_required prevents anonymous users from calling logout_user(), which could cause unexpected behavior or errors.