Automated Action aae9527254 Set up user authentication flow with FastAPI and SQLite
- Created user model with SQLAlchemy ORM
- Implemented authentication with JWT tokens (access and refresh tokens)
- Added password hashing with bcrypt
- Created API endpoints for registration, login, and user management
- Set up Alembic for database migrations
- Added health check endpoint
- Created role-based access control (standard users and superusers)
- Added comprehensive documentation
2025-06-10 15:58:57 +00:00

108 lines
3.8 KiB
Markdown

# User Authentication Service
A FastAPI service for user authentication with JWT tokens.
## Features
- User registration and management
- Authentication with JWT tokens (access and refresh tokens)
- Role-based access control (standard users and superusers)
- Password hashing with bcrypt
- SQLite database with SQLAlchemy ORM
- Alembic migrations
## Getting Started
### Prerequisites
- Python 3.10+
- pip (Python package manager)
### Installation
1. Clone the repository
2. Install dependencies:
```bash
pip install -r requirements.txt
```
3. Run database migrations:
```bash
alembic upgrade head
```
4. Start the server:
```bash
uvicorn main:app --reload
```
The API will be available at http://localhost:8000
## API Documentation
Once the server is running, you can access the interactive API documentation at:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
## Environment Variables
The following environment variables can be set in a `.env` file:
| Variable | Description | Default Value |
|-----------------------------|--------------------------------------------------|-------------------------|
| SECRET_KEY | JWT secret key | Auto-generated |
| ACCESS_TOKEN_EXPIRE_MINUTES | Access token expiration time in minutes | 30 |
| REFRESH_TOKEN_EXPIRE_DAYS | Refresh token expiration time in days | 7 |
| SQLALCHEMY_DATABASE_URL | Database connection string | SQLite in /app/storage |
## Authentication Flow
1. **Registration**: Users can register via `POST /api/v1/register/`
2. **Login**: Users can obtain tokens via `POST /api/v1/auth/login`
3. **Access Protected Resources**: Include the access token in the Authorization header (`Bearer {token}`)
4. **Refresh Token**: When the access token expires, use `POST /api/v1/auth/refresh-token` to get a new one
## Project Structure
```
.
├── alembic.ini # Alembic configuration
├── app # Application package
│ ├── api # API endpoints
│ │ ├── deps.py # API dependencies
│ │ └── v1 # API version 1
│ │ ├── api.py # API router
│ │ └── endpoints # API endpoint modules
│ ├── core # Core modules
│ │ ├── config.py # Configuration settings
│ │ └── security.py # Security utilities
│ ├── crud # CRUD operations
│ │ └── user.py # User CRUD operations
│ ├── db # Database
│ │ ├── base.py # Base class
│ │ ├── base_class.py # Base class imports
│ │ ├── base_model.py # Base model
│ │ ├── init_db.py # Database initialization
│ │ └── session.py # Database session
│ ├── models # SQLAlchemy models
│ │ └── user.py # User model
│ └── schemas # Pydantic schemas
│ ├── token.py # Token schemas
│ └── user.py # User schemas
├── main.py # FastAPI application
├── migrations # Alembic migrations
│ ├── env.py # Alembic environment
│ ├── README # Alembic README
│ ├── script.py.mako # Migration script template
│ └── versions # Migration versions
├── pyproject.toml # Project configuration
└── requirements.txt # Python dependencies
```
## License
This project is licensed under the MIT License