0
0
Flaskframework~10 mins

Why authentication matters in Flask - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Flask class.

Flask
from flask import [1]
app = [1](__name__)
Drag options to blanks, or click blank then click option'
ASession
BRequest
CBlueprint
DFlask
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Request' instead of 'Flask' to create the app.
2fill in blank
medium

Complete the code to protect a route with a login check.

Flask
@app.route('/dashboard')
def dashboard():
    if not session.get('[1]'):
        return 'Please log in first', 401
    return 'Welcome to your dashboard!'
Drag options to blanks, or click blank then click option'
Auser
Blogged_in
Cauthenticated
Dusername
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'user' which may store username but not login status.
3fill in blank
hard

Fix the error in the login function to set the session correctly.

Flask
from flask import session

def login():
    # after verifying user credentials
    session['[1]'] = True
    return 'Logged in successfully!'
Drag options to blanks, or click blank then click option'
Alogged_in
Buser
Cauthenticated_user
Dusername
Attempts:
3 left
💡 Hint
Common Mistakes
Setting 'user' or 'username' to True instead of a boolean flag.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that stores usernames and their login status.

Flask
user_status = {user: session.get('[1]', False) for user in users if user [2] 'admin'}
Drag options to blanks, or click blank then click option'
Alogged_in
B!=
C==
Dauthenticated
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' to exclude 'admin'.
5fill in blank
hard

Fill all three blanks to create a secure logout function that clears the session and redirects.

Flask
from flask import session, redirect, url_for

def logout():
    session.[1]()
    return redirect(url_for('[2]'))

@app.route('/logout')
def logout_route():
    return logout()
Drag options to blanks, or click blank then click option'
Aclear
Blogin
Cremove
Dhome
Attempts:
3 left
💡 Hint
Common Mistakes
Using session.remove() which does not exist.