Back to Python
2026-03-295 min read

Sign in to track progress

Learn Sign in to track progress step by step with clear examples and exercises.

Title: Sign In to Track Progress - Python Web Development

Why This Matters

In web applications, it's essential to keep track of user progress and activities. One common feature is the sign-in system that allows users to access their personal data, preferences, and saved information. In this lesson, we will learn how to implement a simple sign-in functionality using Python with Flask, a popular micro web framework.

A well-implemented sign-in system provides several benefits:

  1. Personalization: Users can customize their experience by saving settings and preferences.
  2. Security: By requiring authentication, you protect sensitive user data from unauthorized access.
  3. User Retention: Sign-in systems help users manage multiple accounts more easily, encouraging them to return to your application.
  4. Analytics: With user tracking, you can gather valuable insights about user behavior and improve the overall user experience.

Prerequisites

Before diving into the sign-in process, make sure you have:

  1. Basic knowledge of Python programming (variables, functions, loops, conditionals)
  2. Familiarity with web development concepts (HTML, CSS, and JavaScript are beneficial but not required for this lesson)
  3. Installation of Flask (pip install flask)
  4. Understanding of hashing algorithms like SHA-256 (though we'll discuss it in more detail later)

Core Concept

To create a sign-in system, we will use Flask's built-in functions to handle user input and manage sessions. Here's an overview of the steps involved:

  1. Create routes for the login page and processing the form data.
  2. Implement a simple authentication mechanism such as comparing entered passwords with stored hashed versions.
  3. Use Flask sessions to keep track of logged-in users.
  4. Store user data in a database (optional, not covered in this lesson).
  5. Secure your application by using HTTPS and implementing CSRF protection (discussed later).

Worked Example

Let's create a simple sign-in application with two files: app.py and templates/login.html.

project_folder/
├── app.py
├── templates/
│ └── login.html

app.py

from flask import Flask, render_template, request, session, redirect, url_for, flash
import hashlib

app = Flask(__name__)

Set a secret key for sessions

app.secret_key = "your-secret-key"

users = {

"user1": "hashed_password1",

"user2": "hashed_password2",

}

def hash_password(password):

Replace this with a more secure hashing algorithm like bcrypt

return hashlib.sha256(password.encode()).hexdigest()

@app.route('/')

def login():

if session.get("logged_in"):

return redirect(url_for('dashboard'))

return render_template('login.html')

@app.route('/login', methods=['POST'])

def login_process():

entered_password = request.form["password"]

user = None

for username, hashed_password in users.items():

if hash_password(entered_password) == hashed_password:

user = username

break

if user:

session["logged_in"] = True

session["username"] = user

return redirect(url_for('dashboard'))

else:

flash("Invalid username or password", "error")

return render_template('login.html')

@app.route('/dashboard')

def dashboard():

if not session.get("logged_in"):

return redirect(url_for('login'))

return f'Welcome, {session["username"]}!'

if __name__ == "__main__":

app.run(debug=True)


**templates/login.html**

Sign In

{% if error %}

{{ error }}

{% endif %}

Username:

Password:

Sign In


In the above example, we've added Flask's `flash()` function to display error messages. We also used a CSS class "error" to style them for better readability.

Common Mistakes

  1. Forgetting to set the secret key for sessions.
  2. Using plain text passwords instead of hashed versions.
  3. Not checking if the user is already logged in before showing the login page.
  4. Failing to redirect users to the dashboard after successful sign-in.
  5. Neglecting error handling and displaying appropriate messages.
  6. Not securing your application with HTTPS or implementing CSRF protection.
  7. Using weak hashing algorithms like SHA-256, which can be easily cracked by attackers.
  8. Storing user data in plain text instead of hashed versions.
  9. Failing to validate user input (e.g., checking for empty fields or incorrect formats).
  10. Leaking sensitive information through error messages or logs.

Practice Questions

  1. Modify the example to handle multiple users with different roles (e.g., admin, user).
  2. Implement a password reset feature for forgotten passwords.
  3. Create a registration form for new users.
  4. Store user data in a database instead of using an in-memory dictionary.
  5. Secure your application by using HTTPS and implementing CSRF protection.
  6. Implement input validation to ensure that all fields are filled out correctly.
  7. Improve the security of your sign-in system by using a more secure hashing algorithm like bcrypt.
  8. Add functionality to log users out when they choose to do so.
  9. Implement a feature to remember usernames for future sessions.
  10. Create a forgot username feature to help users who have forgotten their login credentials.

FAQ

Q: Why is it important to use hashed passwords instead of plain text versions?

A: Hashing passwords makes them unreadable and more secure, as attackers cannot easily reverse the hash to obtain the original password. Using a strong hashing algorithm like bcrypt further increases security by salted hashes, making it even harder for attackers to crack passwords.

Q: How can I improve the security of my sign-in system further?

A: Use a more secure hashing algorithm like bcrypt, store salted hashes, and implement CSRF protection. Additionally, you should validate user input, sanitize data, and avoid storing sensitive information in plain text.

Q: What is Flask's session mechanism, and how does it help in building web applications?

A: Flask sessions allow you to store data associated with the user during their session, making it easier to maintain state between requests. This enables features like user authentication, personalization, and more.

Q: What is CSRF protection, and why is it important for web applications?

A: Cross-Site Request Forgery (CSRF) is an attack where a malicious website tricks the user's browser into making unintended requests to the application. Implementing CSRF protection helps prevent such attacks by adding tokens to forms that are verified on submission, ensuring that only legitimate requests are processed.

Q: How can I secure my application with HTTPS?

A: To secure your application with HTTPS, you'll need an SSL certificate. You can purchase one from a trusted Certificate Authority (CA) or use Let's Encrypt for free. Once you have the certificate, configure your web server to use it and force all traffic to be served over HTTPS.

Sign in to track progress | Python | XQA Learn