Challenge - 5 Problems
Flask-Login Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a user accesses a @login_required route without being logged in?
Consider a Flask app using Flask-Login with a route decorated by @login_required. What is the behavior when an anonymous user tries to access this route?
Flask
from flask import Flask from flask_login import LoginManager, login_required app = Flask(__name__) login_manager = LoginManager(app) @login_manager.unauthorized_handler def unauthorized(): return 'Redirected to login', 302 @app.route('/dashboard') @login_required def dashboard(): return 'Welcome to your dashboard!'
Attempts:
2 left
💡 Hint
Think about what @login_required does when the user is not authenticated.
✗ Incorrect
The @login_required decorator checks if the user is logged in. If not, it triggers the unauthorized handler, which usually redirects to the login page.
❓ state_output
intermediate1:30remaining
What is the value of current_user.is_authenticated after login?
After a user successfully logs in using Flask-Login's login_user(user) function, what is the value of current_user.is_authenticated?
Flask
from flask_login import current_user, login_user # Assume user is a valid User object login_user(user) value = current_user.is_authenticated
Attempts:
2 left
💡 Hint
Check what current_user.is_authenticated means after login.
✗ Incorrect
After login_user(user) is called, current_user represents the logged-in user, so is_authenticated is True.
📝 Syntax
advanced2:30remaining
Which code snippet correctly sets up user_loader for Flask-Login?
You want to tell Flask-Login how to load a user from a user ID stored in the session. Which snippet correctly implements the user_loader callback?
Attempts:
2 left
💡 Hint
Flask-Login expects a function that returns a user object or None given a user ID string.
✗ Incorrect
The user_loader function must accept a user ID string and return the user object. Using User.query.get(int(user_id)) is the standard pattern with SQLAlchemy.
🔧 Debug
advanced3:00remaining
Why does current_user always appear anonymous even after login?
You call login_user(user) successfully, but in your routes current_user.is_authenticated is always False. What is the most likely cause?
Flask
from flask import Flask from flask_login import LoginManager, login_user, current_user app = Flask(__name__) login_manager = LoginManager(app) @app.route('/login') def login(): user = User.query.first() login_user(user) return 'Logged in' @app.route('/profile') def profile(): if current_user.is_authenticated: return 'User profile' else: return 'Anonymous user'
Attempts:
2 left
💡 Hint
Flask-Login requires a user_loader callback to retrieve the user from the session ID.
✗ Incorrect
The @login_manager.user_loader function is not defined. Without it, even if the user ID is stored in the session, Flask-Login cannot load the actual user object and defaults to an anonymous user on subsequent requests.
🧠 Conceptual
expert2:00remaining
What is the purpose of the @login_manager.unauthorized_handler decorator?
In Flask-Login, what does the function decorated with @login_manager.unauthorized_handler do?
Attempts:
2 left
💡 Hint
Think about what happens when @login_required denies access.
✗ Incorrect
The unauthorized_handler function is called when a user tries to access a login_required route but is not authenticated. It usually redirects or returns an error.