Flask Tutorials: Remember Me Functionality

beginner
17 min

Flask Tutorials: Remember Me Functionality

Welcome to our comprehensive guide on Flask's Remember Me Functionality! In this tutorial, we'll walk you through creating a login system with session management, focusing on the remember me feature. By the end, you'll have a solid understanding of this powerful tool and be able to implement it in your own projects.

What is Flask's Remember Me Functionality?

Flask's remember me feature allows users to stay logged in to their account even after closing their browser. It does this by setting a persistent cookie on the user's computer, which helps Flask recognize the user on their subsequent visits. Let's dive into the details!

šŸ“ Note: Before we begin, make sure you have Flask installed:

bash
pip install flask

Creating a Basic Flask Application

First, let's create a simple Flask application with a login page.

python
from flask import Flask, render_template, redirect, url_for, request, session app = Flask(__name__) app.secret_key = "your_secret_key" @app.route('/') def home(): if 'username' in session: return render_template('welcome.html') else: return render_template('index.html') @app.route('/login', methods=['POST', 'GET']) def login(): if request.method == 'POST': username = request.form['username'] if username == 'admin': session['username'] = username return redirect(url_for('home')) return render_template('login.html') @app.route('/logout') def logout(): session.pop('username', None) return redirect(url_for('home'))

In the code above, we've created a Flask app with a home route, a login route, and a logout route. The home route checks if the user is logged in by looking for the username key in the session dictionary. If the user is logged in, they are redirected to the welcome page.

šŸ’” Pro Tip: A secret key is used to sign the session data, enhancing its security. Be sure to use a unique secret key in your applications.

Adding the Remember Me Feature

Now let's add the remember me feature to our login page.

python
@app.route('/login', methods=['POST', 'GET']) def login(): if request.method == 'POST': username = request.form['username'] remember = request.form.get('remember') if remember: session.permanent = True if username == 'admin': session['username'] = username return redirect(url_for('home')) return render_template('login.html')

In the updated login function, we added a new variable remember that captures the state of the remember me checkbox. If the checkbox is checked, we set the permanent attribute of the session to True. This means that the session will persist indefinitely, even after the user closes their browser.

Creating the Login Template

We'll need a simple login form for our application. Create a folder named templates and add a file called login.html inside.

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <h1>Login</h1> <form method="POST" action="{{ url_for('login') }}"> <label for="username">Username:</label> <input type="text" name="username"> <br> <label for="remember">Remember me:</label> <input type="checkbox" name="remember" value="1"> <br> <button type="submit">Login</button> </form> </body> </html>

In the template above, we've created a simple login form with a username input, a remember me checkbox, and a submit button.

Testing Our Application

Now that we've built our application, let's test it! Run the following command in your terminal:

bash
flask run

Open a web browser and navigate to http://127.0.0.1:5000/. You should see the login form. Enter admin as the username, check the remember me checkbox, and click Login. You'll be redirected to the welcome page, and the browser will save the session data for future visits.

Quiz Time!

Quick Quiz
Question 1 of 1

Why should you use a unique secret key in your Flask applications?

That's it for our Flask Remember Me Functionality tutorial! Now you can build secure and user-friendly login systems in your projects. Happy coding! šŸŽ‰

šŸŽÆ Next Steps:

  • Add error handling to the login function
  • Implement password protection for user accounts
  • Explore Flask's login manager for more advanced authentication features