A robust, production-ready authentication and user management backend built with FastAPI, featuring modern security best practices, JWT authentication, Redis-based token blocklisting, and secure password reset via email.
-
User Registration & Login
Secure user registration and login with strong password validation and hashing. -
JWT Authentication
- Access and refresh tokens using industry-standard JWTs.
- Tokens are signed with a strong secret and have configurable expiry.
-
Token Revocation (Logout) with Redis
- On logout, tokens are blocklisted in Redis, preventing reuse until expiry.
- No in-memory blocklist: scalable and production-ready.
-
Role-Based Access Control
- User roles (
user,admin) with admin-only endpoints for user management.
- User roles (
-
Password Reset via Email
- Secure, time-limited password reset tokens.
- Email sent via SMTP with a reset link (configurable).
-
Input Validation & Sanitization
- All user input is validated and sanitized using Pydantic.
- Strong password and username requirements.
-
Database Security
- SQLAlchemy ORM with parameterized queries to prevent SQL injection.
- Unique constraints on email and username.
-
Environment-Based Configuration
- All secrets, SMTP, and Redis settings are loaded from environment variables or
.envfile.
- All secrets, SMTP, and Redis settings are loaded from environment variables or
-
CORS & Security Headers
- CORS enabled for frontend integration.
- (Optional) Security headers middleware for HTTP responses.
-
Error Handling
- Custom exception handler to avoid leaking sensitive information.
-
Password Hashing:
All passwords are hashed usingbcryptbefore storage. Plain-text passwords are never saved. -
JWT Security:
- Tokens are signed with a strong, secret key.
- Each token has a unique identifier (
jti) and expiry. - Blocklisted tokens are stored in Redis and checked on every request.
-
Token Blocklisting:
- On logout, the token's
jtiis stored in Redis with an expiry matching the token's remaining lifetime. - Prevents reuse of tokens after logout, even if stolen.
- On logout, the token's
-
Password Reset Security:
- Password reset tokens are generated with a separate secret and are time-limited.
- Reset links are sent via email, and the API never reveals if an email exists (prevents enumeration).
-
Input Validation:
- Usernames must be alphanumeric.
- Passwords must be at least 8 characters and include a special character.
- Emails are validated for proper format.
-
Role-Based Access:
- Admin-only endpoints are protected and return 403 for non-admins.
-
Environment Secrets:
- All secrets and credentials are loaded from environment variables or a
.envfile, never hardcoded.
- All secrets and credentials are loaded from environment variables or a
app/
├── auth/
│ ├── blocklist.py # Redis-based JWT blocklist
│ ├── routes.py # Auth endpoints (register, login, logout, etc.)
│ └── utils.py # Auth utilities (hashing, token creation, etc.)
├── users/
│ └── routes.py # Admin user management endpoints
├── health/
│ └── routes.py # Health check endpoint
├── config.py # Centralized configuration
├── database.py # SQLAlchemy setup
├── models.py # ORM models
├── schemas.py # Pydantic schemas
└── main.py # FastAPI app entry point
requirements.txt
.env.example # Example environment config
git clone <your-repo-url>
cd <project-directory>pip install -r requirements.txtCreate a .env file in the root directory (see .env.example):
SECRET_KEY=your_strong_jwt_secret
PASSWORD_RESET_TOKEN_SECRET=your_password_reset_secret
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_DB=0
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your_email@gmail.com
SMTP_PASSWORD=your_email_password
EMAIL_FROM=your_email@gmail.com
EMAIL_FROM_NAME=Secure App
Make sure you have a Redis server running locally or update the config for your environment.
uvicorn app.main:app --reloadPOST /auth/register— Register a new userPOST /auth/login— Login and receive JWT tokensPOST /auth/logout— Logout and revoke the current tokenPOST /auth/refresh— Refresh access token using a refresh tokenGET /auth/me— Get current user infoPOST /auth/forgot-password— Request password reset (email sent)POST /auth/reset-password— Reset password with tokenGET /users/— List all users (admin only)PUT /users/{user_id}/role— Update user role (admin only)DELETE /users/{user_id}— Delete user (admin only)GET /health/— Health check
MIT
Pull requests and issues are welcome!
Questions?
Open an issue or contact the maintainer.