A secure, scalable, and production-ready Authentication REST API built with FastAPI following modern backend architecture principles.
The project provides a complete authentication system with JWT-based authentication, refresh token management, password security, email verification, password recovery, and a clean service-oriented architecture.
Modern applications require secure identity management. This project provides a complete authentication backend that can be integrated into:
- Web applications
- Mobile applications
- SaaS platforms
- Internal enterprise systems
- Microservice architectures
The API focuses on:
✅ Security ✅ Scalability ✅ Maintainability ✅ Clean architecture ✅ Developer experience
- User registration
- Secure login
- JWT Access Token authentication
- Refresh Token authentication
- Token rotation
- Token revocation
- Logout current session
- Logout all sessions
- Protected routes
- User profile retrieval
- Profile update
- Password change
- Account management
- UUID-based user identities
- Argon2 password hashing
- JWT token signing
- Secure token expiration
- Password strength validation
- SQL injection protection
- Input validation
- CORS configuration
- Security headers
- Rate limiting
- Environment-based configuration
- Email verification
- Verification tokens
- Password reset emails
- Secure recovery flow
- Automatic Swagger documentation
- Async database support
- Repository pattern
- Service layer architecture
- Dependency injection
- Structured logging
- Custom exception handling
- Docker support
- Automated testing
The application follows a layered architecture:
Router Layer
|
↓
Service Layer
|
↓
Repository Layer
|
↓
Database Layer
Handles:
- HTTP requests
- Request validation
- Response formatting
Location:
app/routers/
Contains business logic:
- Authentication workflow
- User management
- Token generation
- Email handling
Location:
app/services/
Handles database operations:
- CRUD operations
- Queries
- Data persistence
Location:
app/repositories/
auth-api/
│
├── app/
│
│ ├── config/
│ │ ├── __init__.py
│ │ └── settings.py
│
│ ├── core/
│ │ ├── database.py
│ │ ├── exceptions.py
│ │ └── logging.py
│
│ ├── models/
│ │ ├── base.py
│ │ ├── user.py
│ │ ├── refresh_token.py
│ │ ├── password_reset_token.py
│ │ └── email_verification_token.py
│
│ ├── schemas/
│ │ ├── common.py
│ │ ├── user.py
│ │ └── auth.py
│
│ ├── repositories/
│ │ ├── base.py
│ │ ├── user.py
│ │ └── token.py
│
│ ├── services/
│ │ ├── user.py
│ │ ├── auth.py
│ │ ├── token.py
│ │ └── email.py
│
│ ├── routers/
│ │ ├── auth.py
│ │ ├── user.py
│ │ └── health.py
│
│ ├── dependencies/
│ │ ├── auth.py
│ │ ├── database.py
│ │ └── services.py
│
│ ├── security/
│ │ ├── password.py
│ │ ├── jwt.py
│ │ └── tokens.py
│
│ ├── middleware/
│ │ ├── logging.py
│ │ ├── rate_limit.py
│ │ └── security_headers.py
│
│ ├── utils/
│ │ ├── validators.py
│ │ └── helpers.py
│
│ ├── tests/
│ │ ├── conftest.py
│ │ ├── test_auth.py
│ │ ├── test_user.py
│ │ └── test_validators.py
│
│ └── main.py
│
├── migrations/
│ ├── env.py
│ ├── script.py.mako
│ └── versions/
│
├── .env.example
├── .gitignore
├── Dockerfile
├── docker-compose.yml
├── requirements.txt
├── pyproject.toml
├── alembic.ini
├── Makefile
└── README.md
Register
|
↓
Password Hash (Argon2)
|
↓
Store User
|
↓
Email Verification
|
↓
Login
|
↓
Generate JWT Access Token
Generate Refresh Token
|
↓
Access Protected APIs
Used for API authentication.
Example:
Authorization: Bearer <access_token>
Recommended lifetime:
15 minutes
Used to generate new access tokens.
Features:
- Database stored
- Revocable
- Rotated after usage
- Device/session based
Recommended lifetime:
30 days
| Method | Endpoint | Description |
|---|---|---|
| POST | /auth/register |
Create account |
| POST | /auth/login |
Login |
| POST | /auth/refresh |
Refresh token |
| POST | /auth/logout |
Logout session |
| POST | /auth/logout-all |
Logout all devices |
| POST | /auth/verify-email |
Verify email |
| POST | /auth/forgot-password |
Request password reset |
| POST | /auth/reset-password |
Reset password |
| Method | Endpoint | Description |
|---|---|---|
| GET | /users/me |
Current user |
| PATCH | /users/me |
Update profile |
| PATCH | /users/password |
Change password |
| Method | Endpoint |
|---|---|
| GET | /health |
git clone https://github.com/ItsWanheda/auth-api.git
cd auth-apipython -m venv .venvActivate:
Linux:
source .venv/bin/activateWindows:
.venv\Scripts\activatepip install -r requirements.txtCopy:
cp .env.example .envExample:
DATABASE_URL=postgresql://user:password@localhost/authdb
JWT_SECRET_KEY=your_secret_key
ACCESS_TOKEN_EXPIRE_MINUTES=15
REFRESH_TOKEN_EXPIRE_DAYS=30
SMTP_HOST=smtp.example.com
SMTP_PORT=587
SMTP_USERNAME=
SMTP_PASSWORD=Run:
alembic upgrade headCreate migration:
alembic revision --autogenerate -m "migration_name"uvicorn app.main:app --reloadServer:
http://localhost:8000
Swagger:
http://localhost:8000/docs
ReDoc:
http://localhost:8000/redoc
Build:
docker compose buildRun:
docker compose upProduction:
docker compose up -dRun tests:
pytestCoverage:
pytest --cov=appUsing Makefile:
make installmake testmake formatmake lint- OAuth2 login
- Google authentication
- GitHub authentication
- Discord authentication
- Two-factor authentication
- WebAuthn / Passkeys
- Device management
- Login history
- Suspicious login detection
- Advanced rate limiting
- Audit logs
- Admin dashboard
- User roles
- Permissions system
- API keys
- Microservice support
Contributions are welcome.
Steps:
- Fork repository
- Create feature branch
git checkout -b feature/new-feature- Commit changes
git commit -m "Add new feature"- Push changes
git push origin feature/new-feature- Open Pull Request
This project is licensed under the MIT License.
Built with ❤️ by ItsWanheda using:
- Python
- FastAPI
- PostgreSQL
- SQLAlchemy
- Modern Backend Architecture
If this project helped you, consider giving it a ⭐ on GitHub.