Skip to content

FastAuth

Pluggable, NextAuth-inspired authentication for FastAPI. Mount a complete auth system — providers, adapters, JWT, RBAC — in a few lines, without locking you into an ORM.

Install

pip install "sreekarnv-fastauth[standard]"

standard includes FastAPI, joserfc + cryptography (JWT), SQLAlchemy + aiosqlite, and argon2-cffi. Other extras: oauth, webauthn, email, redis, postgresql, cli, all. See Installation.

The smallest FastAuth app

from contextlib import asynccontextmanager
from fastapi import Depends, FastAPI
from fastauth import FastAuth, FastAuthConfig
from fastauth.adapters.sqlalchemy import SQLAlchemyAdapter
from fastauth.api.deps import require_auth
from fastauth.providers.credentials import CredentialsProvider
from fastauth.types import UserData

adapter = SQLAlchemyAdapter(engine_url="sqlite+aiosqlite:///./auth.db")

auth = FastAuth(FastAuthConfig(
    secret="change-me-in-production-min-32-bytes!!",
    providers=[CredentialsProvider()],
    adapter=adapter.user,
    token_adapter=adapter.token,
))

@asynccontextmanager
async def lifespan(app: FastAPI):
    await adapter.create_tables()
    yield

app = FastAPI(lifespan=lifespan)
auth.mount(app)

@app.get("/dashboard")
async def dashboard(user: UserData = Depends(require_auth)):
    return {"hello": user["email"]}

Run with uvicorn main:app --reload.

Next steps