0
0
Flaskframework~20 mins

Login form and verification in Flask - 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 submits the login form with correct credentials?

Consider a Flask login form that checks username and password against stored values. What is the typical behavior after a successful login?

Flask
from flask import Flask, request, redirect, url_for, render_template_string
app = Flask(__name__)

users = {'alice': 'wonderland'}

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        if username in users and users[username] == password:
            return redirect(url_for('dashboard'))
        else:
            return 'Login Failed', 401
    return render_template_string('''<form method='post'>
        <input name='username'>
        <input name='password' type='password'>
        <input type='submit'>
    </form>''')

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

if __name__ == '__main__':
    app.run()
AThe user is redirected to the dashboard page with a welcome message.
BThe login form reloads with an error message about invalid credentials.
CThe server returns a 404 Not Found error.
DThe user sees the login form again without any message.
Attempts:
2 left
💡 Hint

Think about what redirect(url_for('dashboard')) does after a successful login.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Flask login route

Which option correctly fixes the syntax error in this Flask login route code?

Flask
from flask import Flask, request
app = Flask(__name__)

users = {'alice': 'wonderland'}

@app.route('/login', methods=['POST'])
def login():
    username = request.form['username']
    password = request.form['password']
    if username in users and users[username] == password
        return 'Logged in'
    else:
        return 'Failed'

if __name__ == '__main__':
    app.run()
AChange 'request.form' to 'request.forms'.
BIndent the return statements one level less.
CAdd a colon ':' at the end of the if statement line.
DRemove the else block entirely.
Attempts:
2 left
💡 Hint

Python requires a colon at the end of control statements like if.

state_output
advanced
2:00remaining
What is the output after submitting invalid credentials?

Given this Flask login snippet, what response does the server send when the password is wrong?

Flask
from flask import Flask, request
app = Flask(__name__)

users = {'bob': 'builder'}

@app.route('/login', methods=['POST'])
def login():
    username = request.form['username']
    password = request.form['password']
    if username in users and users[username] == password:
        return 'Success'
    else:
        return 'Unauthorized', 401

if __name__ == '__main__':
    app.run()
AA redirect to the login page
B'Unauthorized' with HTTP status code 401
C'Success' with HTTP status code 200
DA server error 500
Attempts:
2 left
💡 Hint

Look at the else block's return statement and its status code.

🔧 Debug
advanced
2:00remaining
Why does this Flask login form always fail to authenticate?

Examine the code below. Why does the login always fail even with correct credentials?

Flask
from flask import Flask, request
app = Flask(__name__)

users = {'eve': 'secret'}

@app.route('/login', methods=['POST'])
def login():
    username = request.form.get('username')
    password = request.form.get('password')
    if username == users.get(username) and password == users.get(username):
        return 'Logged in'
    else:
        return 'Failed'

if __name__ == '__main__':
    app.run()
AThe route does not accept POST requests.
BThe form data keys are incorrect; should use 'user' and 'pass'.
CThe users dictionary is empty, so no match is possible.
DThe condition compares username to password instead of checking password correctness.
Attempts:
2 left
💡 Hint

Look carefully at the if condition comparing username and password.

🧠 Conceptual
expert
3:00remaining
What is the main security risk of this simple Flask login implementation?

Consider a Flask login system that stores passwords in plain text in a dictionary and checks them directly. What is the biggest security risk?

Flask
users = {'admin': '12345'}

@app.route('/login', methods=['POST'])
def login():
    username = request.form['username']
    password = request.form['password']
    if username in users and users[username] == password:
        return 'Welcome'
    else:
        return 'Denied', 401
APasswords are stored in plain text, risking exposure if the code or data leaks.
BThe login form uses POST instead of GET, which is insecure.
CThe code does not use HTTPS, causing syntax errors.
DThe username is not validated for length, causing runtime errors.
Attempts:
2 left
💡 Hint

Think about how passwords should be stored securely.