A secure authentication system built with FastAPI, featuring JWT tokens, refresh tokens, and token blacklisting.
- User registration and login
- JWT-based authentication with access and refresh tokens
- Secure password hashing with bcrypt
- Refresh token rotation
- Token blacklisting for logout
- Email validation
- Protected routes
- SQLite database with SQLModel ORM
- Python 3.7+
- pip (Python package installer)
- Clone the repository:
git clone <repository-url>
cd fastapi-jwt-auth
- Install dependencies:
pip install -r requirements.txt
- Create a
.env
file in the root directory with the following variables:
SECRET_KEY=your-secret-key
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
REFRESH_TOKEN_EXPIRE_DAYS=7
- Start the server:
fastapi dev main.py
- Access the API documentation at
http://localhost:8000/docs
POST /register
- Register a new userPOST /login
- Login and get access tokenGET /current_user
- Get current user detailsGET /users
- Get all users (protected)POST /refresh
- Refresh access tokenPOST /logout
- Logout and invalidate tokens
- HTTP-only cookies for refresh tokens
- Secure password hashing
- Token blacklisting
- Email validation
- Access token expiration
- Refresh token rotation
The project uses SQLite with SQLModel ORM. The database file (database.db
) will be created automatically when you first run the application.
Run tests using pytest:
pytest test.py