This project is a simple FastAPI web application that provides a basic login functionality using a MySQL database. It allows users to enter their username and password and checks if the provided credentials match any entry in the MySQL database. If the credentials are valid, it returns a "Login successful" message; otherwise, it raises an HTTP 401 "Invalid credentials" error.
To set up and run this project, follow the steps below:
Python: Make sure you have Python installed on your system. This project requires Python 3.6 or higher. MySQL: You need a running MySQL server. Ensure that you have the necessary credentials (username and password) to connect to the MySQL server. If you don't have MySQL installed, you can download and install it from the official website: https://dev.mysql.com/downloads/installer/
- Clone this repository to your local machine:
git clone <repository-url>
cd <repository-folder>
- Create a virtual environment:
python -m venv venv
- Activate the virtual environment: On Windows:
venv\Scripts\activate
On macOS and Linux:
source venv/bin/activate
- Install the required Python packages:
pip install -r requirements.txt
The application creates a test table 'users' with some sample data to demonstrate the login functionality.
To run the FastAPI application, execute the following command:
uvicorn app:app --reload
The '--reload' option enables automatic code reloading during development, so you don't have to restart the server after each code change.
Once the application is running, you can access it in your web browser by navigating to 'http://127.0.0.1:8000/' The login page will be displayed.
- Enter the username and password in the respective input fields.
- Click on the "Login" button.
- The form data will be sent to the server using a POST request to /login/.
- If the provided credentials are valid, you will see a "Login successful" message.
- If the credentials are invalid, you will see an "Invalid credentials" message.
- This is a simple demonstration of login functionality.
- The database connection details are hardcoded in the main.py file for demonstration purposes.
- The 'mysql.connector' library is used in this project for MySQL database connection.