0
0
Flaskframework~10 mins

Email verification pattern in Flask - Interactive Code Practice

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]
Drag options to blanks, or click blank then click option'
AFlask
BRequest
Crender_template
Dredirect
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of Flask
Using lowercase flask instead of Flask
2fill in blank
medium

Complete the code to define a route for email verification.

Flask
@app.route('/verify_email/[1]')
def verify_email(token):
    pass
Drag options to blanks, or click blank then click option'
Atoken
Bemail
Cuser
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name in the route and function
Not including a variable in the route
3fill in blank
hard

Fix the error in the code to generate a timed token for email verification.

Flask
from itsdangerous import URLSafeTimedSerializer

def generate_token(email):
    serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
    return serializer.[1](email, salt=app.config['SECURITY_PASSWORD_SALT'])
Drag options to blanks, or click blank then click option'
Asign
Bloads
Cdumps
Dunsign
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'loads' instead of 'dumps' to create a token
Using 'sign' or 'unsign' which are not methods of URLSafeTimedSerializer
4fill in blank
hard

Fill both blanks to decode the token and handle expiration.

Flask
def confirm_token(token, expiration=3600):
    serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
    try:
        email = serializer.[1](token, salt=app.config['SECURITY_PASSWORD_SALT'], max_age=[2])
    except Exception:
        return False
    return email
Drag options to blanks, or click blank then click option'
Aloads
Bexpiration
Csign
Dunsign
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dumps' to decode the token
Not passing the expiration time correctly
5fill in blank
hard

Fill all three blanks to send a verification email with a token link.

Flask
from flask_mail import Message

def send_verification_email(user_email):
    token = generate_token(user_email)
    verify_url = url_for('verify_email', token=[1], _external=True)
    msg = Message('Please verify your email', sender=app.config['MAIL_DEFAULT_SENDER'], recipients=[user_email])
    msg.body = f'Click the link to verify your email: [2]'
    mail.[3](msg)
Drag options to blanks, or click blank then click option'
Atoken
Bverify_url
Csend
Dsend_message
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send_message' instead of 'send'
Passing the wrong variable to url_for
Not using the verification URL in the email body