0
0
Flaskframework~20 mins

Current_user object in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Current_user Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What does current_user.is_authenticated return?
In a Flask app using Flask-Login, what is the value of current_user.is_authenticated when a user is logged in?
ARaises AttributeError
BFalse
CNone
DTrue
Attempts:
2 left
💡 Hint
Think about what Flask-Login sets when a user is logged in.
state_output
intermediate
2:00remaining
What is the output of accessing current_user.name before login?
Given this Flask route:
from flask_login import current_user

@app.route('/')
def index():
    return current_user.name

What happens if no user is logged in and this route is accessed?
Flask
from flask_login import current_user

@app.route('/')
def index():
    return current_user.name
AReturns 'AnonymousUserMixin'
BRaises AttributeError
CReturns None
DReturns empty string
Attempts:
2 left
💡 Hint
Anonymous users do not have a 'name' attribute by default.
📝 Syntax
advanced
1:30remaining
Which code correctly checks if current_user is authenticated and has role 'admin'?
Select the option that correctly checks if the logged-in user is authenticated and has the role 'admin'. Assume current_user.role holds the user's role as a string.
Aif current_user.is_authenticated and current_user.role == 'admin':
Bif current_user.is_authenticated or current_user.role == 'admin':
Cif current_user.is_authenticated and current_user.role = 'admin':
Dif current_user.is_authenticated & current_user.role == 'admin':
Attempts:
2 left
💡 Hint
Use the correct logical operator and comparison syntax.
🔧 Debug
advanced
2:00remaining
Why does current_user always appear anonymous in this Flask app?
A developer notices that current_user.is_authenticated is always False even after login. Which of these is the most likely cause?
AThe user_loader callback is not set up correctly
BThe Flask app is missing a secret key
CThe login route does not call login_user()
DThe current_user object is not imported
Attempts:
2 left
💡 Hint
Flask-Login needs to reload user from session using user_loader.
🧠 Conceptual
expert
2:30remaining
What is the role of current_user in Flask-Login's request lifecycle?
Choose the best description of how current_user works during a Flask request when using Flask-Login.
A<code>current_user</code> is a global variable set once at app startup and never changes
B<code>current_user</code> is a function that must be called to get the user object
C<code>current_user</code> is a proxy that loads the user from the session on each request and provides user info
D<code>current_user</code> stores the user password in plain text for quick access
Attempts:
2 left
💡 Hint
Think about how Flask-Login manages user state per request.