0
0
Flaskframework~20 mins

Flask-Login extension - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask-Login Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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!'
AThe user is redirected to the login page or unauthorized handler is called.
BThe server crashes with an exception.
CThe server returns a 404 Not Found error.
DThe user sees the dashboard content without logging in.
Attempts:
2 left
💡 Hint
Think about what @login_required does when the user is not authenticated.
state_output
intermediate
1: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
ATrue
BFalse
CNone
DRaises an AttributeError
Attempts:
2 left
💡 Hint
Check what current_user.is_authenticated means after login.
📝 Syntax
advanced
2: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?
A
@login_manager.user_loader
def load_user(user_id):
    return User.get(user_id)
B
@login_manager.user_loader
def load_user(user_id):
    return User.load(user_id)
C
@login_manager.user_loader
def load_user(user_id):
    return User.find_by_id(user_id)
D
@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))
Attempts:
2 left
💡 Hint
Flask-Login expects a function that returns a user object or None given a user ID string.
🔧 Debug
advanced
3: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'
AThe Flask app is missing a secret key for session management.
BThe user object passed to login_user is None.
CThe @login_manager.user_loader function is not defined.
DThe login_user function is called outside a request context.
Attempts:
2 left
💡 Hint
Flask-Login requires a user_loader callback to retrieve the user from the session ID.
🧠 Conceptual
expert
2: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?
AIt logs out the current user when unauthorized access is detected.
BIt defines the response when a user tries to access a protected route without being logged in.
CIt automatically redirects logged-in users to the home page.
DIt initializes the login manager with the Flask app.
Attempts:
2 left
💡 Hint
Think about what happens when @login_required denies access.