
- Create user model and database connection - Set up Alembic migrations - Implement JWT token authentication - Add routes for registration, login, refresh, and user profile - Create health endpoint - Configure CORS - Update README with setup and usage instructions
109 lines
2.5 KiB
Markdown
109 lines
2.5 KiB
Markdown
# User Authentication Service
|
|
|
|
A FastAPI service for user authentication with JWT tokens.
|
|
|
|
## Features
|
|
|
|
- User registration and login
|
|
- JWT token-based authentication
|
|
- Token refresh functionality
|
|
- Password hashing with bcrypt
|
|
- SQLite database with SQLAlchemy ORM
|
|
- Alembic migrations
|
|
- CORS support
|
|
- Health endpoint
|
|
|
|
## Prerequisites
|
|
|
|
- Python 3.9+
|
|
- pip (Python package manager)
|
|
|
|
## Setup
|
|
|
|
1. Clone the repository:
|
|
|
|
```bash
|
|
git clone <repository-url>
|
|
cd userauthenticationservice-0fe432
|
|
```
|
|
|
|
2. Create and activate a virtual environment (optional but recommended):
|
|
|
|
```bash
|
|
python -m venv venv
|
|
source venv/bin/activate # On Windows: venv\Scripts\activate
|
|
```
|
|
|
|
3. Install dependencies:
|
|
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
4. Create a `.env` file based on the `.env.example`:
|
|
|
|
```bash
|
|
cp .env.example .env
|
|
```
|
|
|
|
5. Edit the `.env` file and set a secure secret key:
|
|
|
|
```
|
|
SECRET_KEY=your_secure_secret_key
|
|
```
|
|
|
|
6. Run database migrations:
|
|
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
## Running the Service
|
|
|
|
Start the service with:
|
|
|
|
```bash
|
|
uvicorn main:app --reload
|
|
```
|
|
|
|
The API will be available at http://localhost:8000
|
|
|
|
API documentation is available at:
|
|
- Swagger UI: http://localhost:8000/docs
|
|
- ReDoc: http://localhost:8000/redoc
|
|
|
|
## API Endpoints
|
|
|
|
- `POST /api/v1/auth/register` - Register a new user
|
|
- `POST /api/v1/auth/login` - Login and get access token
|
|
- `POST /api/v1/auth/refresh` - Refresh access token
|
|
- `GET /api/v1/auth/me` - Get current user information
|
|
- `PUT /api/v1/auth/me` - Update current user information
|
|
- `GET /health` - Health check endpoint
|
|
|
|
## Environment Variables
|
|
|
|
| Variable | Description | Default |
|
|
|----------|-------------|---------|
|
|
| SECRET_KEY | JWT signing key | supersecretkey |
|
|
| ALGORITHM | JWT algorithm | HS256 |
|
|
| ACCESS_TOKEN_EXPIRE_MINUTES | Access token lifetime in minutes | 30 |
|
|
| REFRESH_TOKEN_EXPIRE_DAYS | Refresh token lifetime in days | 7 |
|
|
| DATABASE_URL | SQLite database URL | sqlite:///app/storage/db/db.sqlite |
|
|
|
|
## Authentication Flow
|
|
|
|
1. Register a user with `POST /api/v1/auth/register`
|
|
2. Login with `POST /api/v1/auth/login` to get access and refresh tokens
|
|
3. Use the access token in the `Authorization` header for protected endpoints
|
|
4. When the access token expires, use `POST /api/v1/auth/refresh` with the refresh token to get a new access token
|
|
|
|
## Development
|
|
|
|
This project uses:
|
|
- FastAPI for the API framework
|
|
- SQLAlchemy for ORM
|
|
- Alembic for database migrations
|
|
- Pydantic for data validation
|
|
- python-jose for JWT handling
|
|
- passlib and bcrypt for password hashing |