
- 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
3.8 KiB
3.8 KiB
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
- Clone the repository
- Install dependencies:
pip install -r requirements.txt
- Run database migrations:
alembic upgrade head
- Start the server:
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
- Registration: Users can register via
POST /api/v1/register/
- Login: Users can obtain tokens via
POST /api/v1/auth/login
- Access Protected Resources: Include the access token in the Authorization header (
Bearer {token}
) - 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