Skip to main content

Production-ready FastAPI auth system with JWT, refresh tokens, and async DB support

Project description

FastAPI Auth 🔐

Production-ready authentication system for FastAPI with async support, JWT, refresh tokens, and pluggable database backends.


🚀 Features

  • ⚡ Fully async (no blocking I/O)

  • 🔐 JWT authentication (access + refresh tokens)

  • 🔁 Refresh token flow

  • 🚪 Logout with token blacklist

  • 🧱 Multi-database support:

    • PostgreSQL (asyncpg)
    • MySQL (aiomysql)
    • SQLite (aiosqlite)
    • MongoDB (motor)
  • 🧠 Dependency-based auth (FastAPI native)

  • 📦 Plug-and-play integration

  • 🛡️ Password hashing with Argon2 (modern standard)

  • 📄 Clean OpenAPI (Swagger) docs with Pydantic schemas


📦 Installation

pip install "fastapi-async-auth-kit[<db>]"

With database support

pip install "fastapi-async-auth-kit[postgres]"
pip install "fastapi-async-auth-kit[mysql]"
pip install "fastapi-async-auth-kit[mongodb]"
pip install "fastapi-async-auth-kit[sqlite]"

⚙️ Quick Start

Step 1: How to initiate auth on startup

from fastapi import FastAPI
from fastapi_async_auth_kit import init_auth, AuthConfig

app = FastAPI()

@app.on_event("startup")
# for postgres
async def startup():
    await init_auth(
        app,
        AuthConfig(
            secret_key="your-secret",
            db_url="postgresql+asyncpg://user:pass@localhost/fastapi_auth",
            db_type="postgres"
        )
    )

# for mysql
async def startup():
    await init_auth(
        app,
        AuthConfig(
            secret_key="secret",
            db_url="mysql+aiomysql://admin:Admin123@localhost:3306/fastapi_auth",
            db_type="mysql"
        )
    )

# for sqlite
async def startup():
    await init_auth(
        app,
        AuthConfig(
            secret_key="my-secret-key",
            db_url="sqlite+aiosqlite:///./fastapi_auth.db",
            db_type="sqlite"
        )
    )

# for mongo db
async def startup():
    await init_auth(
        app,
        AuthConfig(
            secret_key="super-secret-key",
            db_url="mongodb://localhost:27017/fastapi_auth",
            db_type="mongodb"
        )
    )

Step 2: How to validate token for all your FastAPIs

from fastapi import APIRouter, Depends
from fastapi_async_auth_kit.dependencies.auth import get_current_user
router = APIRouter()

@router.get("/user")
async def me(user=Depends(get_current_user)):
    return user

🔐 Available Endpoints

Endpoint Description
POST /auth/register Register new user
POST /auth/login Login and get tokens
POST /auth/refresh Refresh access token
POST /auth/logout Logout and revoke token
GET /auth/me Get current user

Swagger UI

🧪 Example

Login

POST /auth/login

{
  "username": "john",
  "password": "Strong@123"
}

Response

{
  "access": "jwt_token",
  "refresh": "refresh_token"
}

🧠 Architecture

FastAPI App
   ↓
Auth Service
   ↓
Repository Layer
   ↓
Database (Async)

🛡️ Security

  • Argon2 password hashing
  • JWT token expiration
  • Refresh token blacklist
  • No sensitive data exposure
  • Clean error handling

🧩 Extensibility

  • Add RBAC (roles & permissions)
  • Plug custom user models
  • Add OAuth providers (Google, GitHub)
  • Integrate Redis for token storage

🛠 Tech Stack

  • FastAPI
  • SQLAlchemy (async)
  • Pydantic
  • python-jose (JWT)
  • Argon2 (password hashing)

📌 Roadmap

  • RBAC support
  • Redis token blacklist
  • OAuth integration
  • Rate limiting
  • Email verification

🤝 Contributing

Pull requests are welcome. For major changes, open an issue first.

Source Code: https://github.com/kmistry1110/fastapi_auth


📄 License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_async_auth_kit-0.10.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_async_auth_kit-0.10.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_async_auth_kit-0.10.0.tar.gz.

File metadata

  • Download URL: fastapi_async_auth_kit-0.10.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for fastapi_async_auth_kit-0.10.0.tar.gz
Algorithm Hash digest
SHA256 0bda9d9076cb3daba32f4f7d6d30fd96a582321b688cdbe7b20b01e48255afa4
MD5 95d2ea226c4d84f10ba1e5e41f12e93a
BLAKE2b-256 a9d7bf2bcb35dbc4b84fddeb112df1b9645eaf0e1d679e5d5dd4595f6de9db77

See more details on using hashes here.

File details

Details for the file fastapi_async_auth_kit-0.10.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_async_auth_kit-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3624dfb1d7e516c2adcdc2454f3c89b9c5e29d5f2b654ddf80dfc8c049fa8b6c
MD5 dd2189cdb8b4f3eacddd00d603ed2b82
BLAKE2b-256 781a39df901eee6f5bb47e914040acdd51b769dc799b949faa4765c835ead44b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page