A secure authentication system built with FastAPI, featuring user management, JWT token-based authentication, and database integration.
- User Authentication: JWT-based token authentication
- User Management: Create and manage users
- Password Security: Secure password hashing and verification
- Database Integration: SQLAlchemy ORM with database session management
- Role-Based Access: Core security and dependencies for access control
- API Routing: Organized routing for auth and user endpoints
- Python 3.8+
- FastAPI
- SQLAlchemy
- Pydantic
- python-jose (for JWT tokens)
- passlib (for password hashing)
- Clone the repository:
git clone https://github.com/s33m0/FastAPI-Secure-Auth.git
cd FastAPI-Secure-Auth- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Create a
.envfile in the project root:
cp .env.example .env # if available, or create manuallyAll sensitive and environment-specific variables are stored in a .env file instead of being hardcoded in the source code.
This includes:
- Database connection URL
- Secret keys
- JWT configuration
- API keys
- Debug mode
The application loads these variables through the configuration module using Pydantic BaseSettings.
For development, copy .env.example to .env and provide your own values.
⚠️ The.envfile is excluded from version control for security reasons.
app/
├── auth/ # Authentication routes and services
│ ├── router.py
│ ├── schemas.py
│ └── services.py
├── users/ # User management routes and services
│ ├── model.py
│ ├── router.py
│ ├── schemas.py
│ └── services.py
├── core/ # Core configuration and security
│ ├── config.py
│ ├── dependencies.py
│ └── security.py
├── database/ # Database configuration
│ ├── base.py
│ ├── session.py
│ └── __init__.py
└── main.py # Application entry point
- Start the development server:
uvicorn app.main:app --reload- Access the API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
POST /auth/login- Login and get access tokenPOST /auth/register- Register a new user
GET /users- List all usersGET /users/{user_id}- Get user detailsPUT /users/{user_id}- Update userDELETE /users/{user_id}- Delete user
Configuration is managed in app/core/config.py. Update settings as needed for your environment.
The application uses SQLAlchemy ORM for database operations. Configure your database connection in the .env file or config.py.
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
For issues and questions, please open an issue on the GitHub repository.