
- 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
79 lines
1.9 KiB
Markdown
79 lines
1.9 KiB
Markdown
# 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
|
|
|
|
1. Install dependencies:
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
2. Run database migrations:
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
3. Start the application:
|
|
```bash
|
|
uvicorn main:app --reload
|
|
```
|
|
|
|
## API Endpoints
|
|
|
|
### Public Endpoints
|
|
- `GET /` - Service information
|
|
- `GET /health` - Health check
|
|
- `POST /api/v1/auth/register` - User registration
|
|
- `POST /api/v1/auth/login` - User login
|
|
|
|
### Protected Endpoints (require Bearer token)
|
|
- `GET /api/v1/users/me` - Get current user info
|
|
- `GET /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
|
|
|
|
1. Register a new user:
|
|
```bash
|
|
curl -X POST "http://localhost:8000/api/v1/auth/register" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"email": "user@example.com", "password": "password123"}'
|
|
```
|
|
|
|
2. Login to get access token:
|
|
```bash
|
|
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"
|
|
```
|
|
|
|
3. Access protected endpoint:
|
|
```bash
|
|
curl -X GET "http://localhost:8000/api/v1/users/me" \
|
|
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
|
|
```
|