FastAPI application with SQLAlchemy ORM, SQL Server connection, and JWT (JSON Web Token) authentication.
- Modern FastAPI framework with automatic API documentation
- User registration and authentication
- JWT access tokens and refresh tokens
- SQL Server database integration
- RESTful API endpoints with OpenAPI/Swagger docs
- Type hints and Pydantic models
- Async support
- CORS middleware
- User profile management
- Token validation and refresh
app/
├── models/
│ └── user.py # SQLAlchemy User model with token fields
├── routes/
│ └── auth_routes.py # FastAPI authentication endpoints
├── services/
│ ├── auth_service.py # Authentication business logic
│ └── jwt_service.py # JWT token management
├── schemas.py # Pydantic models for request/response
├── dependencies.py # FastAPI dependencies for authentication
├── __init__.py # FastAPI app factory
└── database.py # Database configuration
-
Install dependencies:
pip install -r requirements.txt
-
Configure database:
- Copy
.env.exampleto.env - Update
DATABASE_URLwith your SQL Server connection string - Set
JWT_SECRET_KEYfor token signing
- Copy
-
Run the application:
python serve.py
Or using uvicorn directly:
uvicorn serve:app --host 0.0.0.0 --port 8000 --reload
-
Access API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
POST /api/auth/register- Register new userPOST /api/auth/login- Login and get JWT tokensPOST /api/auth/refresh- Refresh access token using refresh tokenPOST /api/auth/logout- Logout and revoke tokensGET /api/auth/profile- Get user profile (requires JWT token)PUT /api/auth/profile- Update user profile (requires JWT token)GET /api/auth/users- Get all users (requires JWT token)POST /api/auth/validate-token- Validate JWT token (fast validation)GET /api/auth/token-info- Get information about current JWT token
curl -X POST http://localhost:8000/api/auth/register \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"email": "test@example.com",
"password": "password123",
"first_name": "Test",
"last_name": "User"
}'curl -X POST http://localhost:8000/api/auth/login \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"password": "password123"
}'curl -X GET http://localhost:8000/api/auth/profile \
-H "Authorization: Bearer YOUR_JWT_ACCESS_TOKEN"curl -X POST http://localhost:8000/api/auth/validate-token \
-H "Content-Type: application/json" \
-d '{
"token": "YOUR_JWT_ACCESS_TOKEN"
}'curl -X POST http://localhost:8000/api/auth/refresh \
-H "Content-Type: application/json" \
-d '{
"refresh_token": "YOUR_JWT_REFRESH_TOKEN"
}'| Field | Type | Description |
|---|---|---|
| id | Integer | Primary key |
| username | String(50) | Unique username |
| String(100) | Unique email | |
| password_hash | String(255) | Hashed password |
| first_name | String(50) | First name |
| last_name | String(50) | Last name |
| is_active | Boolean | Account status |
| access_token | Text | Current access token |
| refresh_token | Text | Current refresh token |
| token_expires_at | DateTime | Access token expiration |
| refresh_token_expires_at | DateTime | Refresh token expiration |
| created_at | DateTime | Account creation date |
| updated_at | DateTime | Last update date |
| last_login_at | DateTime | Last login date |
- Access tokens: JWT tokens with 1 hour expiration
- Refresh tokens: JWT tokens with 30 day expiration
- Token Structure: Contains user information (user_id, username, email, etc.)
- Security: HMAC SHA256 algorithm for signing
- Validation: Fast token validation without database lookup available
- Revocation: Tokens can be revoked by removing from database
Access Token contains:
user_id: User's database IDusername: User's usernameemail: User's emailfirst_name: User's first namelast_name: User's last nameis_active: User's active statustype: "access"exp: Expiration timestampiat: Issued at timestampnbf: Not before timestamp
Refresh Token contains:
user_id: User's database IDusername: User's usernametype: "refresh"exp: Expiration timestampiat: Issued at timestampnbf: Not before timestamp
- Python 3.7+
- SQL Server
- ODBC Driver 17 for SQL Server
- FastAPI and Uvicorn for ASGI server
- PyJWT library for JWT token handling
- Pydantic for data validation
- Automatic API Documentation: Swagger UI and ReDoc available at
/docsand/redoc - Type Safety: Full type hints support with Pydantic models
- Performance: High performance, on par with NodeJS and Go
- Modern Python: Uses modern Python features (Python 3.6+ type hints)
- Standards-based: Based on OpenAPI and JSON Schema
- Async Support: Native async/await support
- Dependency Injection: Advanced dependency injection system
- Data Validation: Automatic request/response validation