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.7.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.7.0
File Size Uploaded Provenance Actions
fastapi_myauth-1.7.0.tar.gz 188.1 kB With provenance Details for fastapi_myauth-1.7.0.tar.gz

Built distribution (wheel)

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

Total release size: 256.6 kB

Release files / fastapi_myauth-1.7.0.tar.gz

Download URL fastapi_myauth-1.7.0.tar.gz
Size 188.1 kB
Tags Source
SHA-256 checksum
How to use checksums
1d954223222b8d83631fe8e61a4613a31d1378e508ac2d4527702ceba3f7cc5c
BLAKE2b-256 checksum
How to use checksums
c6aacbe04a31b8590d5e4f99b55c1347612d6a70e73fecdd155a8383e1a480fe
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 15, 2026.

Transparency log

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

Download URL fastapi_myauth-1.7.0-py3-none-any.whl
Size 68.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5ecf4ddd4ce218c41df320e1d28952fbed785c6035d573c6fed679b3570b0923
BLAKE2b-256 checksum
How to use checksums
25aa1af2e984e315298050cd01ffa8a373363ca0706a421a21202dced6aabe0c
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 15, 2026.

Transparency log

Security for fastapi-myauth 1.7.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.7.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 15, 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