0
0
Flaskframework~30 mins

Remember me functionality in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Remember Me Functionality in Flask
📖 Scenario: You are building a simple login page for a website using Flask. You want to add a "Remember Me" checkbox so that users can stay logged in even after closing their browser.
🎯 Goal: Create a Flask app that has a login form with a "Remember Me" checkbox. When the user logs in and checks this box, their login session should persist even after closing the browser. If unchecked, the session should end when the browser closes.
📋 What You'll Learn
Create a Flask app with a login route
Add a login form with username, password, and a "Remember Me" checkbox
Use Flask's session management to remember the user based on the checkbox
Set the session to be permanent only if "Remember Me" is checked
💡 Why This Matters
🌍 Real World
Remember Me functionality is common on websites to improve user experience by keeping users logged in across browser sessions.
💼 Career
Understanding session management and user authentication is essential for backend web development roles.
Progress0 / 4 steps
1
Set up Flask app and login route
Create a Flask app instance called app and define a route /login that accepts both GET and POST methods.
Flask
Need a hint?

Use Flask(__name__) to create the app and decorate a function with @app.route('/login', methods=['GET', 'POST']).

2
Add login form HTML with Remember Me checkbox
Inside the login function, create a string variable called login_form that contains HTML for a form with fields: username, password, and a checkbox named remember labeled "Remember Me". The form should submit to /login using POST.
Flask
Need a hint?

Use a multi-line string with ''' to store the HTML form. Include a checkbox input with name="remember".

3
Implement login POST logic with Remember Me session setting
Inside the login function, check if the request method is POST. If yes, get the username and check if the remember checkbox is in request.form. Set session['username'] to the username. Set session.permanent to True if remember is checked, otherwise False. Then redirect to /dashboard. If GET, return the login_form using render_template_string.
Flask
Need a hint?

Check request.method. Use session.permanent = True if remember is checked. Redirect after login.

4
Add dashboard route to show logged-in user
Create a new route /dashboard that shows a welcome message with the logged-in username from the session. If no user is logged in, redirect to /login.
Flask
Need a hint?

Check if session['username'] exists. Show welcome message or redirect to login.