
- Set up FastAPI application with SQLite database - Create User model with email and password fields - Implement JWT token-based authentication - Add user registration and login endpoints - Create protected user profile endpoints - Configure Alembic for database migrations - Add password hashing with bcrypt - Include CORS middleware and health endpoint - Update README with setup and usage instructions Environment variables required: - SECRET_KEY: JWT secret key for token signing
User Authentication Service
A FastAPI-based user authentication service with JWT token authentication and SQLite database.
Features
- User registration and login
- JWT token-based authentication
- Password hashing with bcrypt
- SQLite database with SQLAlchemy ORM
- Database migrations with Alembic
- CORS enabled for all origins
- Health check endpoint
- Auto-generated API documentation
Environment Variables
Set the following environment variables before running the application:
SECRET_KEY
: JWT secret key for token signing (required for production)
Installation
- Install dependencies:
pip install -r requirements.txt
- Run database migrations:
alembic upgrade head
- Start the application:
uvicorn main:app --reload
API Endpoints
Public Endpoints
GET /
- Service informationGET /health
- Health checkPOST /api/v1/auth/register
- User registrationPOST /api/v1/auth/login
- User login
Protected Endpoints (require Bearer token)
GET /api/v1/users/me
- Get current user infoGET /api/v1/users/profile
- Get user profile
API Documentation
Once the application is running, visit:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- OpenAPI JSON: http://localhost:8000/openapi.json
Usage Example
- Register a new user:
curl -X POST "http://localhost:8000/api/v1/auth/register" \
-H "Content-Type: application/json" \
-d '{"email": "user@example.com", "password": "password123"}'
- Login to get access token:
curl -X POST "http://localhost:8000/api/v1/auth/login" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=user@example.com&password=password123"
- Access protected endpoint:
curl -X GET "http://localhost:8000/api/v1/users/me" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
Description
Languages
JavaScript
100%