What is the main purpose of the Flask-Login extension?
Flask-Login helps manage user sessions in Flask apps. It keeps track of logged-in users and handles login, logout, and session protection easily.
Click to reveal answer
beginner
Which function in Flask-Login is used to log a user in?
The login_user() function logs a user in by creating a session for them.
Click to reveal answer
beginner
What does the @login_required decorator do?
It protects routes so only logged-in users can access them. If a user is not logged in, it redirects them to the login page.
Click to reveal answer
intermediate
How does Flask-Login identify the current user in a request?
Flask-Login provides a current_user proxy that represents the logged-in user or an anonymous user if no one is logged in.
Click to reveal answer
intermediate
What must a user class implement to work with Flask-Login?
The user class must have these methods: <code>is_authenticated</code>, <code>is_active</code>, <code>is_anonymous</code>, and a method <code>get_id()</code> that returns a unique ID as a string.
Click to reveal answer
Which Flask-Login function logs out the current user?
Alogout_user()
Blogin_user()
Ccurrent_user()
Duser_loader()
✗ Incorrect
The logout_user() function ends the user session and logs the user out.
What does the user_loader callback do in Flask-Login?
ALoads a user from the database by ID
BLogs a user in
CLogs a user out
DProtects routes
✗ Incorrect
The user_loader callback tells Flask-Login how to get a user object from a user ID stored in the session.
What happens if a non-logged-in user tries to access a route with @login_required?
AThey get a 404 error
BThey see the page anyway
CThe app crashes
DThey are redirected to the login page
✗ Incorrect
The @login_required decorator redirects users who are not logged in to the login page.
Which attribute tells if a user is logged in or not?
Acurrent_user.is_active
Bcurrent_user.is_authenticated
Ccurrent_user.get_id()
Dcurrent_user.is_anonymous
✗ Incorrect
is_authenticated is True if the user is logged in, False otherwise.
What type of value should get_id() return?
AA list of permissions
BA boolean
CA unique string identifying the user
DAn integer count
✗ Incorrect
get_id() must return a unique string that identifies the user for session management.
Explain how Flask-Login manages user sessions in a Flask app.
Think about how users log in, stay logged in, and how routes can be protected.
You got /5 concepts.
Describe the requirements for a user class to work with Flask-Login.
Focus on the methods Flask-Login expects to check user status and identity.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the Flask-Login extension in a Flask application?
easy
A. To create database models for users
B. To manage user sessions and handle login/logout functionality
C. To style the login page with CSS
D. To send emails to users after login
Solution
Step 1: Understand Flask-Login's role
Flask-Login is designed to manage user sessions, making it easy to log users in and out securely.
Step 2: Compare options with Flask-Login's purpose
Creating database models, styling pages, or sending emails are not handled by Flask-Login.
Final Answer:
To manage user sessions and handle login/logout functionality -> Option B
Quick Check:
Flask-Login manages user sessions = A [OK]
Hint: Remember: Flask-Login handles login sessions, not styling or emails [OK]
Common Mistakes:
Confusing Flask-Login with database or styling tools
Thinking Flask-Login sends emails
Assuming it creates user database models
2. Which of the following is the correct way to initialize Flask-Login's LoginManager in a Flask app using an application factory pattern?
easy
A. login_manager = LoginManager(); login_manager.init_app(app)
B. login_manager = LoginManager(app=app)
C. login_manager = LoginManager(app)
D. login_manager = LoginManager.init_app(app)
Solution
Step 1: Recall Flask-Login initialization pattern
The usual pattern is to create a LoginManager instance without arguments, then call init_app(app) to bind it.
Step 2: Check each option's syntax
login_manager = LoginManager(); login_manager.init_app(app) correctly creates the instance and calls init_app(app). login_manager = LoginManager(app) tries to pass app to constructor which is not standard. login_manager = LoginManager(app=app) uses a wrong keyword argument. login_manager = LoginManager.init_app(app) calls init_app as a class method, which is invalid.
Final Answer:
login_manager = LoginManager(); login_manager.init_app(app) -> Option A
Quick Check:
Use init_app() after creating LoginManager = C [OK]
Hint: Create LoginManager first, then call init_app(app) [OK]
Common Mistakes:
Passing app directly to LoginManager constructor
Using wrong argument names
Calling init_app as a class method
3. Given this Flask-Login user loader function, what will load_user('42') return if the user with ID 42 exists in the database?
A. User.get(user_id) should be User.query.get(user_id)
B. login_manager.init_app(app) must be called before defining load_user
C. user_loader decorator is missing parentheses
D. login_manager must be created after app
Solution
Step 1: Check user_loader function correctness
The typical way to get a user by ID in Flask-SQLAlchemy is User.query.get(user_id). Using User.get(user_id) will cause an AttributeError.
Step 2: Verify other parts of the code
Calling init_app(app) after defining user_loader is fine. The decorator syntax is correct. Creating LoginManager before app is allowed if init_app is called later.
Final Answer:
User.get(user_id) should be User.query.get(user_id) -> Option A
Quick Check:
User.get() is invalid, use User.query.get() = B [OK]
Hint: Use User.query.get() to fetch user by ID [OK]
Common Mistakes:
Using User.get() instead of User.query.get()
Thinking init_app must be called before user_loader
Misunderstanding decorator syntax
5. You want to protect a Flask route so only logged-in users can access it. Which is the correct way to do this using Flask-Login?
hard
A. Manually check session variables for user login status
B. Check if current_user.is_authenticated inside the route and redirect if not
C. Use the @login_required decorator on the route function
D. Add login_manager.login_view = 'login' inside the route function
Flask-Login provides a @login_required decorator to easily protect routes so only logged-in users can access them.
Step 2: Evaluate other options
Check if current_user.is_authenticated inside the route and redirect if not is possible but less clean and requires manual redirect logic. Manually check session variables for user login status is low-level and error-prone. Add login_manager.login_view = 'login' inside the route function sets the login page globally but does not protect routes by itself.
Final Answer:
Use the @login_required decorator on the route function -> Option C
Quick Check:
Use @login_required to protect routes = A [OK]
Hint: Use @login_required decorator for easy route protection [OK]
Common Mistakes:
Manually checking login status instead of using decorator
Confusing login_view setting with route protection
Ignoring Flask-Login's built-in decorators
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.