User Authentication Service

A FastAPI service for user authentication using JWT tokens with SQLite database.

Features

  • User registration and login
  • JWT token-based authentication
  • Password hashing with bcrypt
  • Protected routes with authentication
  • SQLite database with SQLAlchemy ORM
  • Alembic migrations

Getting Started

Prerequisites

  • Python 3.9+
  • pip

Installation

  1. Clone the repository:
git clone <repository-url>
cd userauthenticationservice
  1. Install dependencies:
pip install -r requirements.txt
  1. Set up environment variables:

Create a .env file in the project root directory and add the following variables:

SECRET_KEY=your-secret-key-here
ACCESS_TOKEN_EXPIRE_MINUTES=30

Database Setup

Run the database migrations:

alembic upgrade head

Running the Application

Start the FastAPI 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:

Authentication Flow

  1. Register a new user:

    • Endpoint: POST /api/v1/users/
    • Body:
      {
        "email": "user@example.com",
        "username": "username",
        "full_name": "User Name",
        "password": "password123",
        "password_confirm": "password123"
      }
      
  2. Login to get an access token:

    • Endpoint: POST /api/v1/auth/token
    • Form data:
      username: user@example.com
      password: password123
      
    • Response:
      {
        "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
        "token_type": "bearer"
      }
      
  3. Access protected endpoints:

    • Add the header: Authorization: Bearer <access_token>
    • Example protected endpoint: GET /api/v1/users/me

Environment Variables

Variable Description Default
SECRET_KEY Secret key for JWT token generation CHANGEME_SECRET_KEY_CHANGEME
ACCESS_TOKEN_EXPIRE_MINUTES Token expiration time in minutes 30
BACKEND_CORS_ORIGINS CORS allowed origins ["*"]

Project Structure

.
├── alembic.ini
├── app
│   ├── api
│   │   └── v1
│   │       ├── api.py
│   │       └── endpoints
│   │           ├── auth.py
│   │           ├── protected.py
│   │           └── users.py
│   ├── core
│   │   └── config.py
│   ├── db
│   │   ├── init_db.py
│   │   └── session.py
│   ├── models
│   │   └── user.py
│   ├── schemas
│   │   ├── auth.py
│   │   └── user.py
│   └── services
│       ├── auth.py
│       ├── security.py
│       └── user.py
├── main.py
├── migrations
│   ├── env.py
│   ├── script.py.mako
│   └── versions
│       └── 001_create_users_table.py
└── requirements.txt
Description
Project: User Authentication Service
Readme 42 KiB
Languages
Python 97.4%
Mako 2.6%