This project provides a basic user authentication and management system using FastAPI. Users can register, log in, log out, and delete their accounts. Additionally, all requests during these operations are logged and stored in a database.
- Python 3.12+
- SQLite (or another supported database)
- Virtualenv (optional but recommended)
-
Clone this repository:
git clone https://github.com/sakiphan/FastAPI-python.git cd FastAPI-python
-
Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate # macOS/Linux venv\Scripts\activate # Windows
-
Install the required Python packages:
pip install -r requirements.txt
-
Start the FastAPI application:
python run.py
-
To run the HTML file through a server:
python -m http.server 8000
Open your browser and go to http://127.0.0.1:8001
. Here you can register and log in.
- Click on the
Register
button. - Enter the username, email, full name, and password.
- Click the
Register
button. - After successful registration, you will be redirected to the login page.
- Click on the
Login
button. - Enter the username and password.
- Click the
Login
button. - After successful login, you will be redirected to the user actions page.
- Click the
Logout
button. - After successful logout, you will be redirected back to the login page.
- Click the
Delete Account
button. - After successfully deleting your account, you will be redirected to the registration page.
main.py
: The main file for the FastAPI application.auth.py
: Handles authentication and JWT token operations.crud.py
: Database operations (CRUD operations).database.py
: Database connection and session management.logs.py
: Logging database operations.schemas.py
: Pydantic models for request and response validation.middleware.py
: Middleware for logging requests.run.py
: Script to start the FastAPI application.index.html
: HTML file for testing the registration, login, logout, and account deletion.
The HTML file index.html
provides a simple interface to test the API. It includes forms for registration, login, logout, and account deletion, with a basic CSS style.
- Make sure the FastAPI server is running on
http://127.0.0.1:8001
as the HTML file makes requests to this address. - Ensure email validation is enforced to include an
@
symbol as required.