Skip to content

dmm1/fastapi-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

37 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Fastapi-Starter Project - Modern Authentication System

A modern authentication system starter built with FastAPI backend and a React frontend using shadcn/ui, dark mode, avatar management, and a modern admin dashboard. This project provides a solid foundation for production-ready apps with role-based access control, user management, and beautiful UI/UX.

🎯 Purpose: The included React frontend is designed for real-world authentication and admin flows. Easily extend with more features or integrate with your own backend.

πŸš€ Quick Start (Both Servers)

The easiest way to run both backend and frontend simultaneously:

python run.py

This will start:

Press Ctrl+C to stop both servers.

🌟 Features

Frontend (React + shadcn/ui)

  • βœ… Modern UI/UX: Built with shadcn/ui, dark mode, and beautiful design
  • βœ… Avatar Management: Upload, preview, and delete profile avatars
  • βœ… Admin Dashboard: Stats, user management, and role badges
  • βœ… Role-Based Navigation: Dynamic UI based on user roles
  • βœ… Theme Toggle: Built-in dark mode support
  • βœ… Profile Page: Edit profile, change password, manage avatar
  • βœ… Login/Register: Modern forms with validation and error handling
  • βœ… Responsive Design: Works on mobile and desktop
  • βœ… TanStack Router: Modern routing and navigation
  • βœ… API Client: Automatic token refresh and error handling

Backend (FastAPI)

  • βœ… Avatar Upload/Delete: Endpoints for uploading and deleting user avatars
  • βœ… Static File Serving: Avatars served via /static/avatars/
  • βœ… Modern Architecture: Modular FastAPI with separation of concerns
  • βœ… JWT Authentication: Access and refresh tokens with role information
  • βœ… Secure Session Management: Redis-backed session tracking with device information
  • βœ… Role-Based Access Control (RBAC): Flexible role management system
  • βœ… SQLite Database: Persistent storage with SQLAlchemy ORM
  • βœ… User Management: Registration, login, profile updates
  • βœ… Admin Panel: Admin-only user management endpoints
  • βœ… API Versioning: Clean /api/v1/ endpoints
  • βœ… OAuth2 Compatible: Standard OAuth2 password flow
  • βœ… Auto Documentation: Swagger UI at /docs
  • βœ… Enhanced Security: Password policies and security headers
  • πŸ†• Custom Rate Limiting: High-performance rate limiting with configurable limits
  • πŸ†• Session Tracking: Track active sessions with device info and IP addresses
  • πŸ†• Monitoring: Comprehensive metrics and health checks
  • πŸ†• Structured Logging: JSON-formatted security event logging
  • πŸ†• Production Ready: Enhanced security and observability

πŸ“ Project Structure

Fastapi-Starter/
β”œβ”€β”€ run.py                    # 🎯 Start both servers
β”œβ”€β”€ backend/                  # πŸ”§ FastAPI Backend
β”‚   β”œβ”€β”€ run.py               # Backend server entry point
β”‚   β”œβ”€β”€ app/                 # Modular FastAPI application
β”‚   β”œβ”€β”€ data/                # SQLite database & avatar uploads
β”‚   └── requirements.txt     # Python dependencies
└── frontend/                # πŸ§‘β€πŸ’» React Frontend (shadcn/ui)
    β”œβ”€β”€ src/                 # React source code
    β”œβ”€β”€ public/              # Static assets
    β”œβ”€β”€ package.json         # Frontend dependencies
    └── README.md            # Frontend guide

πŸ–ΌοΈ Avatar Management

  • Upload Avatar: Go to Profile, click avatar, upload image (PNG/JPG/GIF up to 5MB)
  • Delete Avatar: Click 'Delete Current' in avatar dialog
  • Avatar Storage: Avatars are stored in backend/data/uploads/avatars/ and served at /static/avatars/
  • Fallback: If no avatar, Dicebear initials are shown

πŸ›‘οΈ Admin Dashboard

  • Stats Cards: Total users, admins, active/inactive users
  • User Table: Avatar, status, roles, actions (edit/delete)
  • Role Badges: Visual role indicators
  • Search: Filter users by name/email

πŸ” Session Management

  • Secure Sessions: Redis-backed session storage with database fallback
  • Device Tracking: Track login device, browser, and IP address information
  • Session List: View all active sessions in user profile
  • Remote Logout: Delete sessions from other devices
  • Automatic Cleanup: Expired sessions are automatically removed
  • Session Cookies: Secure, HttpOnly cookies for enhanced security

⚑ Rate Limiting & Security

  • Custom Rate Limiting: High-performance in-memory rate limiting
  • Configurable Limits: Environment-based rate limit configuration
  • Endpoint-Specific: Different limits for auth, API, and admin endpoints
  • Security Headers: CSRF protection, HSTS, and content security policies
  • Monitoring: Real-time metrics and health checks
  • Admin Protection: Special rate limits for admin operations

πŸ“ API Endpoints (Key additions)

Method Endpoint Description
POST /api/v1/users/me/avatar Upload user avatar
DELETE /api/v1/users/me/avatar Delete user avatar
GET /static/avatars/{filename} Serve avatar image
GET /api/v1/sessions/ Get user's active sessions
DELETE /api/v1/sessions/{session_id} Delete specific session
GET /metrics Application metrics (admin only)

πŸ§‘β€πŸ’» Frontend Usage

  • Login/Register: Modern forms, error handling, dark mode
  • Profile: Edit info, change password, manage avatar, view active sessions
  • Session Management: View and delete active sessions from other devices
  • Admin: Manage users, roles, avatars
  • Theme Toggle: Top right in header

βš™οΈ Configuration

The application supports extensive configuration via environment variables:

Security & Authentication

SECRET_KEY=your-secret-key
SECURE_COOKIES=false  # Set to true in production
HTTPS_ONLY=false      # Set to true in production

Rate Limiting

DEFAULT_RATE_LIMIT=1000 per hour
AUTH_LOGIN_RATE_LIMIT=5 per minute
AUTH_REGISTER_RATE_LIMIT=3 per minute
API_GENERAL_RATE_LIMIT=100 per minute
ADMIN_OPERATIONS_RATE_LIMIT=50 per minute

Session & Redis

REDIS_URL=redis://localhost:6379
ACCESS_TOKEN_EXPIRE_MINUTES=30
REFRESH_TOKEN_EXPIRE_DAYS=7

See backend/.env.example for all available configuration options.

Happy Coding! πŸŽ‰

Built with ❀️ using FastAPI, React, shadcn/ui, and modern web technologies.

About

A comprehensive Fastapi Starter Project with a React Frontend and an Mobile App

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors