Bird
Raised Fist0
Flaskframework~20 mins

Why authentication matters in Flask - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Authentication Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why is authentication important in a Flask web app?

Imagine you have a Flask app where users can see their personal data. Why do you need authentication?

ATo change the app's color scheme
BTo allow anyone to edit all users' data
CTo make the app load faster
DTo make sure only the right user can see their own data
Attempts:
2 left
💡 Hint

Think about privacy and safety of user information.

❓ component_behavior
intermediate
1:30remaining
What happens if a Flask route lacks authentication?

Consider a Flask route that shows user profile info but has no authentication check. What is the likely result?

AAnyone can access any user's profile data
BOnly logged-in users can see their profiles
CThe app crashes with an error
DThe profile page shows a loading spinner forever
Attempts:
2 left
💡 Hint

Think about what happens if no one checks who is visiting the page.

❓ state_output
advanced
2:00remaining
What is the output of this Flask login check code?

Given this Flask snippet, what will be printed if a user is not logged in?

Flask
from flask import Flask, session
app = Flask(__name__)
app.secret_key = 'secret'

@app.route('/dashboard')
def dashboard():
    if 'user_id' in session:
        return 'Welcome user!'
    else:
        return 'Please log in first.'

# Assume session is empty (no user_id)
AKeyError: 'user_id'
B500 Internal Server Error
CPlease log in first.
DWelcome user!
Attempts:
2 left
💡 Hint

Check what happens when 'user_id' is missing in session.

📝 Syntax
advanced
2:00remaining
Which Flask code snippet correctly protects a route with login required?

Choose the code that correctly uses Flask-Login to protect a route so only logged-in users can access it.

A
@app.route('/profile')
def profile():
    if current_user.is_authenticated:
        return 'User Profile'
    else:
        return 'Access Denied'
B
@app.route('/profile')
@login_required
def profile():
    return 'User Profile'
C
@app.route('/profile')
@requires_login
def profile():
    return 'User Profile'
D
@app.route('/profile')
def profile():
    if user.is_logged_in:
        return 'User Profile'
    else:
        return 'Access Denied'
Attempts:
2 left
💡 Hint

Flask-Login provides a decorator named login_required.

🔧 Debug
expert
2:30remaining
Why does this Flask login code raise an error?

Examine the code below. Why does it raise a RuntimeError: Working outside of request context?

Flask
from flask import Flask, session
app = Flask(__name__)
app.secret_key = 'secret'

user_id = session.get('user_id')

@app.route('/')
def home():
    return f'User ID is {user_id}'
ABecause session is accessed outside a request, before any client request happens
BBecause Flask app is not run with debug=True
CBecause the route function is missing a return statement
DBecause secret_key is missing
Attempts:
2 left
💡 Hint

Think about when Flask allows access to session.

Practice

(1/5)
1. Why is authentication important in a Flask web application?
easy
A. It speeds up the loading time of web pages.
B. It verifies the identity of users to protect sensitive data.
C. It automatically fixes bugs in the code.
D. It changes the website's color scheme based on user preference.

Solution

  1. Step 1: Understand the purpose of authentication

    Authentication checks who the user is by verifying their identity, usually with a username and password.
  2. Step 2: Connect authentication to security

    This process protects sensitive data and resources from unauthorized access.
  3. Final Answer:

    It verifies the identity of users to protect sensitive data. -> Option B
  4. Quick Check:

    Authentication = User identity verification [OK]
Hint: Authentication means checking who the user is [OK]
Common Mistakes:
  • Confusing authentication with website speed
  • Thinking authentication changes design
  • Believing authentication fixes bugs automatically
2. Which of the following is the correct way to protect a Flask route with authentication using Flask-Login?
easy
A. @login_required def dashboard(): return 'Welcome to your dashboard!'
B. def login_required(): return 'Welcome!'
C. @app.route('/dashboard') def login_required(): return 'Dashboard'
D. @app.login_required def dashboard(): return 'Dashboard'

Solution

  1. Step 1: Recognize Flask-Login decorator usage

    The correct decorator to protect routes is @login_required placed above the route function.
  2. Step 2: Check syntax and function name

    @login_required def dashboard(): return 'Welcome to your dashboard!' correctly uses @login_required above a function named dashboard that returns a welcome message.
  3. Final Answer:

    @login_required\ndef dashboard():\n return 'Welcome to your dashboard!' -> Option A
  4. Quick Check:

    Use @login_required decorator to protect routes [OK]
Hint: Use @login_required above route functions [OK]
Common Mistakes:
  • Using @app.login_required instead of @login_required
  • Defining login_required as a function instead of a decorator
  • Missing the decorator above the route function
3. Given this Flask route using Flask-Login, what happens when an unauthenticated user tries to access /profile?
@app.route('/profile')
@login_required
def profile():
    return 'User Profile'
medium
A. The user sees 'User Profile' page immediately.
B. The server returns a 404 Not Found error.
C. The user is redirected to the login page.
D. The server crashes with an error.

Solution

  1. Step 1: Understand @login_required behavior

    The @login_required decorator checks if the user is logged in before allowing access.
  2. Step 2: Identify what happens if user is not logged in

    If the user is not authenticated, Flask-Login redirects them to the login page automatically.
  3. Final Answer:

    The user is redirected to the login page. -> Option C
  4. Quick Check:

    Unauthenticated access = redirect to login [OK]
Hint: Unauthenticated users get redirected to login page [OK]
Common Mistakes:
  • Assuming unauthenticated users see the protected page
  • Thinking server returns 404 error
  • Believing server crashes on unauthenticated access
4. What is wrong with this Flask route code that tries to protect a page with authentication?
@app.route('/settings')
def settings():
    @login_required
    return 'Settings Page'
medium
A. The code uses @login_required but Flask-Login is not imported.
B. The route decorator @app.route is missing the URL.
C. The function settings() is missing a return statement.
D. The @login_required decorator is incorrectly placed inside the function.

Solution

  1. Step 1: Check decorator placement

    Decorators like @login_required must be placed above the function definition, not inside it.
  2. Step 2: Verify function syntax

    Placing @login_required inside the function causes a syntax error and does not protect the route.
  3. Final Answer:

    The @login_required decorator is incorrectly placed inside the function. -> Option D
  4. Quick Check:

    Decorators go above functions, not inside [OK]
Hint: Put decorators above function definitions, never inside [OK]
Common Mistakes:
  • Placing decorators inside function body
  • Forgetting to import Flask-Login
  • Missing return statement in route function
5. You want to create a Flask app where only logged-in users can access the /dashboard route, and users who are not logged in should be redirected to /login. Which of the following code snippets correctly implements this behavior using Flask-Login?
hard
A. from flask_login import login_required @app.route('/dashboard') @login_required def dashboard(): return 'Dashboard Page' # Flask-Login automatically redirects unauthenticated users to /login
B. @app.route('/dashboard') def dashboard(): if not current_user.is_authenticated: return redirect('/login') return 'Dashboard Page'
C. @app.route('/dashboard') @login_required def dashboard(): return redirect('/login')
D. @app.route('/dashboard') def dashboard(): return 'Dashboard Page'

Solution

  1. Step 1: Identify Flask-Login usage for route protection

    Using @login_required decorator from Flask-Login protects the route and handles redirection automatically.
  2. Step 2: Check if redirection to /login is automatic

    Flask-Login by default redirects unauthenticated users to the login view, so no manual redirect is needed inside the function.
  3. Final Answer:

    from flask_login import login_required @app.route('/dashboard') @login_required def dashboard(): return 'Dashboard Page' # Flask-Login automatically redirects unauthenticated users to /login -> Option A
  4. Quick Check:

    @login_required protects route and redirects unauthenticated users [OK]
Hint: Use @login_required; Flask-Login handles redirect automatically [OK]
Common Mistakes:
  • Manually redirecting inside the route when @login_required suffices
  • Returning redirect inside a protected route without condition
  • Not protecting the route at all
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard