Automated Action f84493a558 Implement user authentication system with FastAPI and SQLite
- 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
2025-06-02 21:28:50 +00:00

2.5 KiB

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:
git clone <repository-url>
cd userauthenticationservice-0fe432
  1. Create and activate a virtual environment (optional but recommended):
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Create a .env file based on the .env.example:
cp .env.example .env
  1. Edit the .env file and set a secure secret key:
SECRET_KEY=your_secure_secret_key
  1. Run database migrations:
alembic upgrade head

Running the Service

Start the service with:

uvicorn main:app --reload

The API will be available at http://localhost:8000

API documentation is available at:

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