A full-featured Todo application built with FastAPI, featuring user authentication, role-based access control, and a web interface.
π Live Application: https://todoapp-fastapi-wb8m.onrender.com/
Try the application online with the test accounts provided below!
- User Authentication: JWT-based authentication with secure password hashing
- Role-Based Access Control: Admin and regular user roles
- Todo Management: Create, read, update, and delete todos
- Web Interface: HTML templates with Bootstrap styling
- Database Support: PostgreSQL with SQLAlchemy ORM
- Database Migrations: Alembic for database schema management
- Comprehensive Testing: Unit tests for all major components
- Backend: FastAPI (Python web framework)
- Database: PostgreSQL
- ORM: SQLAlchemy
- Authentication: JWT tokens with bcrypt password hashing
- Frontend: HTML templates with Jinja2, Bootstrap CSS
- Database Migrations: Alembic
- Testing: pytest
TodoApp/
βββ main.py # FastAPI application entry point
βββ models.py # SQLAlchemy database models
βββ database.py # Database configuration
βββ dependency.py # Authentication and database dependencies
βββ alembic.ini # Alembic configuration
βββ routers/ # API route handlers
β βββ auth.py # Authentication routes
β βββ todos.py # Todo CRUD operations
β βββ admin.py # Admin-only operations
β βββ users.py # User management
βββ templates/ # HTML templates
β βββ layout.html # Base template
β βββ navbar.html # Navigation component
β βββ login.html # Login page
β βββ register.html # Registration page
β βββ todo.html # Todo list page
β βββ add-todo.html # Add todo form
β βββ edit-todo.html # Edit todo form
βββ static/ # Static assets
β βββ css/ # CSS files
β βββ js/ # JavaScript files
βββ alembic/ # Database migration files
βββ test/ # Test files
id: Primary keyemail: Unique email addressusername: Unique usernamefirst_name: User's first namelast_name: User's last namehashed_password: Bcrypt hashed passwordis_active: Account statusrole: User role (admin/user)phone_number: Contact number
id: Primary keytitle: Todo title (minimum 3 characters)description: Todo description (3-100 characters)priority: Priority level (1-5)complete: Completion statusowner_id: Foreign key to Users table
GET /auth/login-page- Login pageGET /auth/register-page- Registration pagePOST /auth/- Create new userPOST /auth/token- Login and get access token
GET /todos/todo-page- Todo list pageGET /todos/add-todo-page- Add todo pageGET /todos/edit-todo-page/{todo_id}- Edit todo pageGET /todos/- Get all user's todos (API)GET /todos/todo/{todo_id}- Get specific todo (API)POST /todos/create-todo- Create new todoPUT /todos/todo/{todo_id}- Update todoDELETE /todos/todo/{todo_id}- Delete todo
GET /admin/todo- Get all todos (admin only)DELETE /admin/todo/{todo_id}- Delete any todo (admin only)
GET /user/- Get current user infoPOST /user/change-password- Change passwordPUT /user/change-phone-number- Update phone number
GET /healthy- Application health status
- Python 3.8+
- PostgreSQL database
- pip (Python package manager)
- Install PostgreSQL and create a database:
CREATE DATABASE TodoApplicationDatabase;- Update database connection in
database.py:
SQLALCHEMY_DATABASE_URL = "postgresql://username:password@localhost/TodoApplicationDatabase"- Clone the repository:
git clone <repository-url>
cd TodoApp- Create and activate virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install fastapi uvicorn sqlalchemy psycopg2-binary alembic bcrypt python-jose python-multipart jinja2 pytest- Run database migrations:
alembic upgrade head- Start the application:
uvicorn main:app --reloadThe application will be available at http://localhost:8000
For testing purposes, you can create these sample accounts:
Admin Account:
{
"email": "duthng98@gmail.com",
"username": "duthng98",
"first_name": "Duth",
"last_name": "Admin",
"password": "123456",
"role": "admin",
"phone_number": "123456"
}User Account:
{
"email": "duthng99@gmail.com",
"username": "duthng99",
"first_name": "Duth",
"last_name": "User",
"password": "123456",
"role": "user",
"phone_number": "123456789"
}- Navigate to
http://localhost:8000 - Register a new account or login with existing credentials
- Create, edit, and manage your todos
- Set priorities and mark todos as complete
- Register a user via
POST /auth/ - Login to get access token via
POST /auth/token - Use the token in Authorization header:
Bearer <token> - Access protected endpoints
- Admin users can view and delete all todos
- Admin role must be set during user creation
Run the test suite:
pytestTest files are located in the test/ directory and cover:
- Authentication functionality
- Todo CRUD operations
- Admin operations
- User management
- Main application routes
- Password Security: Bcrypt hashing with salt
- JWT Authentication: Secure token-based authentication
- Role-Based Access: Admin and user role separation
- Input Validation: Pydantic models for request validation
- SQL Injection Protection: SQLAlchemy ORM prevents SQL injection
Consider setting these environment variables for production:
SECRET_KEY: JWT signing secret (currently hardcoded)DATABASE_URL: Database connection stringALGORITHM: JWT algorithm (default: HS256)
To create new migrations after model changes:
alembic revision --autogenerate -m "Description of changes"
alembic upgrade head- Create new routes in appropriate router files
- Update models if database changes are needed
- Create database migrations with Alembic
- Add corresponding templates for web interface
- Write tests for new functionality
- Follow FastAPI best practices
- Use dependency injection for database and authentication
- Separate concerns with router modules
- Maintain consistent error handling
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
This project is open source and available under the MIT License.