VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-store-username-and-password-in-flask/

⇱ How to Store Username and Password in Flask using MySQL - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Store Username and Password in Flask using MySQL

Last Updated : 29 May, 2026

Learn how to store usernames and passwords in a Flask web application using MySQL, implement user registration and login functionality, and display a welcome message after successful authentication.

Installation

First create a virtual environment. After creating and activating virtual environment install Flask and Flask-MySQLdb using the following command:

pip install Flask Flask-MySQLdb

To learn how to create and activate a virtual environment, refer to: Python virtual environment.

Template Folder

Create a "Templates" folder and in the templates folder create three files in it:

register.html

This HTML file contains a straightforward registration form that asks for three inputs: username, email address and password.

Output:

👁 registration-mysql-page1
regsister.html

login.html

In login.html, we have two input fields for username and password. If the correct credentials are entered, the user is redirected to the profile page after successful login.

Output:

👁 login-page-mysql
login.html

user.html

This pages displays a welcome message after a successful login.

Output:

👁 registration-mysql-page4
user.html

Setting up Flask app

The complete Flask app for user registration, login and database setup is shown below. For a detailed explanation of creating routes and configuring the database, refer to Login and Registration in Flask.

Explanation:

  • Importing Libraries: import Flask for web handling, flask_mysqldb for MySQL database connectivity, and re for input validation.
  • Flask App Configuration: configures the app, including MySQL connection settings and a secret key for session handling.
  • Login Route (/login): handles user authentication by checking the username and password against the database.
  • Logout Route (/logout): ends the session and redirects to the login page.
  • Registration Route (/register): handles new user registrations by validating input and inserting user details into the database.
  • Running the App: the app runs in debug mode, enabling easy debugging during development.

Run the flask application using command mentioned below in the terminal and visit the development server URL to launch it.

python app.py

Adding Users

After launching the app, register two users:

  1. name = GEEK , email = geek@gfg.org, password = 12345
  2. name = Geek , email = geeks@gfg.org, password = 12345

Now, login to my sql server in the terminal using command:

mysql -u root -p

enter MySQL password when prompted to check if these two users have been added to the table or not.

The contents of the current user_table can be displayed by executing the following SQL query:

USE user_table;
SELECT * FROM user;

Here is the snapshot of the live demonstration:

👁 user_table-sql
User table

Related Articles

Comment
Article Tags:
Article Tags: