Skip to main content

FastAPI MyAuth

Authentication and user management for FastAPI applications using synchronous SQLModel sessions. Requires Python 3.13 or newer.

The package provides email/password and magic-link login, rotating refresh tokens, email confirmation, password recovery, TOTP, role checks, and optional OIDC providers, WebAuthn passkeys, and application rate limiting.

pip install fastapi_myauth
# Optional features, independently installable:
pip install 'fastapi_myauth[social,passkeys,redis]'

Quick start

Save this as main.py. Generate and persist two separate secrets before starting it (see Configuration). The example disables email confirmation for local use; configure SMTP to enable email flows.

from contextlib import asynccontextmanager
import os

from fastapi import Depends, FastAPI
from sqlmodel import SQLModel, create_engine

from fastapi_myauth import models
from fastapi_myauth.auth import AuthComponents, FastAuth
from fastapi_myauth.config import Settings

engine = create_engine("sqlite:///database.db", connect_args={"check_same_thread": False})
config = Settings(
    SECRET_KEY=os.environ["AUTH_SECRET_KEY"],
    TOTP_SECRET_KEY=os.environ["AUTH_TOTP_SECRET_KEY"],
    REQUIRE_EMAIL_VALIDATION=False,
)
components = AuthComponents(config=config)
fast_auth = FastAuth(engine=engine, components=components)


@asynccontextmanager
async def lifespan(_app: FastAPI):
    SQLModel.metadata.create_all(engine)
    yield


app = FastAPI(lifespan=lifespan)
app.include_router(fast_auth.get_router())


@app.get("/protected", response_model=models.UserRead)
def protected(current_user=Depends(fast_auth.deps.get_current_active_user)):
    return current_user

Install a server separately, then run uvicorn main:app --reload. Sign up at POST /login/signup with an email and an 8–64 character password; log in at POST /login/oauth with form fields username and password. Send the returned access token as Authorization: Bearer <token>. Interactive API documentation is available at /docs.

config belongs to AuthComponents; FastAuth receives the engine and components. fast_auth.deps and fast_auth.crud_user are properties. Custom user models must inherit from models.User without table=True: AuthComponents creates the mapped table and relationships. Use fast_auth.User for database queries.

Documentation

Repository layout

fastapi_myauth/             Installable library
  api/endpoints/           Router factories
  api/deps.py              Authentication and session dependencies
  crud/                    Synchronous database operations
  models/                  API schemas and mapped models
  auth.py                  AuthComponents and FastAuth
  config.py                Explicit Pydantic settings
  security.py              Hashing, JWT and TOTP helpers
  services.py              Token issuance and one-time email challenges
  email.py                 SMTP delivery
  email-templates/         MJML sources and packaged HTML
examples/main.py           Local demo, initialized through lifespan
tests/                    Isolated in-memory SQLite integration tests
scripts/                  Coverage and distribution checks
docs/                     Development and usage guides

Development

uv sync --locked --all-extras
make ci

CI checks lint and formatting, runs tests with branch coverage (minimum 90%), and builds and inspects distributions. Tests never remove or reuse an application's database.db.

License

MIT.

Metadata

Release files for fastapi-myauth 1.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-myauth 1.6.0
File Size Uploaded Provenance Actions
fastapi_myauth-1.6.0.tar.gz 169.8 kB With provenance Details for fastapi_myauth-1.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-myauth 1.6.0
File Interpreter ABI Platform Actions
fastapi_myauth-1.6.0-py3-none-any.whl Python 3 none any Details for fastapi_myauth-1.6.0-py3-none-any.whl

Total release size: 234.8 kB

Release files / fastapi_myauth-1.6.0.tar.gz

Download URL fastapi_myauth-1.6.0.tar.gz
Size 169.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4c18b6ef39815322880c83dbacb5b18623d54cd81b56d009ab41b255ab1bb736
BLAKE2b-256 checksum
How to use checksums
fc1cd7c19021f6e1029abc013e9f6a4c9100c35a406c1860c5985eeec2bfa940
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / fastapi_myauth-1.6.0-py3-none-any.whl

Download URL fastapi_myauth-1.6.0-py3-none-any.whl
Size 65.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d360f95e9ca4a6f1e9a1c6dfee47681ff406c5d11d9630a0d505971cf97edb74
BLAKE2b-256 checksum
How to use checksums
64e81c052f191e7b2ae9d4b38ffc8b749adce81aa0fc837c952170cb36d67d78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Security for fastapi-myauth 1.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fastapi_myauth-1.6.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page