0
0
Flaskframework~20 mins

Login_required decorator in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Login Required 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 protected route without login?
Consider a Flask route decorated with @login_required. What is the typical behavior if a user tries to access this route without being logged in?
Flask
from flask import Flask, redirect, url_for
from flask_login import LoginManager, login_required

app = Flask(__name__)
login_manager = LoginManager(app)

@login_manager.unauthorized_handler
def unauthorized():
    return redirect(url_for('login'))

@app.route('/dashboard')
@login_required
def dashboard():
    return 'Welcome to your dashboard!'

@app.route('/login')
def login():
    return 'Please log in.'
AThe user is shown a blank page with no message.
BThe user sees the dashboard content anyway.
CThe user is redirected to the login page.
DThe server returns a 500 Internal Server Error.
Attempts:
2 left
💡 Hint
Think about what the unauthorized_handler does in Flask-Login.
📝 Syntax
intermediate
2:00remaining
Identify the correct way to apply the login_required decorator
Which of the following Flask route definitions correctly applies the @login_required decorator?
A
@app.route('/profile')
def profile():
    return login_required('User Profile')
B
@app.route('/profile')
@login_required
def profile():
    return 'User Profile'
C
@app.route('/profile')
def profile():
@login_required
    return 'User Profile'
D
@login_required
@app.route('/profile')
def profile():
    return 'User Profile'
Attempts:
2 left
💡 Hint
Remember the order of decorators matters in Flask routes.
state_output
advanced
2:00remaining
What is the value of current_user.is_authenticated inside a login_required route?
Inside a Flask route decorated with @login_required, what will current_user.is_authenticated return?
Flask
from flask_login import current_user, login_required

@app.route('/settings')
@login_required
def settings():
    return str(current_user.is_authenticated)
ATrue
BFalse
CNone
DRaises an AttributeError
Attempts:
2 left
💡 Hint
The decorator only allows access if the user is logged in.
🔧 Debug
advanced
2:00remaining
Why does this login_required route cause a redirect loop?
Given this Flask code, why does accessing /profile cause a redirect loop?
Flask
from flask import Flask, redirect, url_for
from flask_login import LoginManager, login_required

app = Flask(__name__)
login_manager = LoginManager(app)

@login_manager.unauthorized_handler
def unauthorized():
    return redirect(url_for('profile'))

@app.route('/profile')
@login_required
def profile():
    return 'User Profile'
AThe login_required decorator is missing on the profile route.
BThe unauthorized handler should return a 404 error instead of redirect.
CThe Flask app is missing a secret key, causing session errors.
DThe unauthorized handler redirects to the same protected route, causing infinite redirects.
Attempts:
2 left
💡 Hint
Check where the unauthorized handler sends users who are not logged in.
🧠 Conceptual
expert
3:00remaining
How does the login_required decorator integrate with Flask's request lifecycle?
Which statement best describes how the @login_required decorator works within Flask's request handling process?
AIt intercepts the request before the route function runs and checks user authentication, redirecting if needed.
BIt modifies the response after the route function runs to add authentication headers.
CIt replaces the route function with a login form rendering function unconditionally.
DIt runs only after the response is sent to the client to log user activity.
Attempts:
2 left
💡 Hint
Think about when decorators run in relation to the route function.