Skip to main content

NextAuth-inspired pluggable authentication for FastAPI

Project description

FastAuth

PyPI License: MIT CI codecov Python 3.11+

NextAuth-inspired pluggable authentication for FastAPI.

FastAuth gives you a complete auth system — credentials, OAuth, passkeys (WebAuthn), magic links, email verification, password reset, RBAC, and JWT — without locking you into any particular database or ORM.


Features

  • Multiple providers — email/password, magic links, Google OAuth, GitHub OAuth, passkeys (WebAuthn)
  • Pluggable adapters — SQLAlchemy (SQLite, PostgreSQL, MySQL) or bring your own
  • JWT & database sessions — stateless tokens or server-side sessions
  • Cookie delivery — HttpOnly, Secure, SameSite out of the box
  • Email flows — verification, password reset, and magic links with customizable transports
  • Custom email templates — drop Jinja2 templates into any directory; unoverridden templates fall back to built-ins
  • RBAC — roles and fine-grained permissions on any route
  • Event hooks — intercept sign-in/sign-up and modify JWT payloads
  • RS256 / JWKS — rotate keys and expose a JWKS endpoint for microservices
  • CLI — scaffold a project, check dependencies, generate secrets

Install

pip install "sreekarnv-fastauth[standard]"
Extra Includes
standard FastAPI, JWT (joserfc), SQLAlchemy, Argon2
oauth httpx (Google, GitHub OAuth)
webauthn py-webauthn (passkeys / FIDO2)
email aiosmtplib, Jinja2
redis redis-py async
postgresql asyncpg
cli typer, rich
all everything

Quick start

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

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

auth = FastAuth(FastAuthConfig(
    secret="change-me",           # fastauth generate-secret
    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)  # registers /auth/register, /auth/login, /auth/logout, …

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

Magic Links

Passwordless sign-in with a one-time link sent to the user's email. No password required — unknown emails are auto-registered on first use.

from fastauth.providers.magic_links import MagicLinksProvider
from fastauth.email_transports.smtp import SMTPTransport

auth = FastAuth(FastAuthConfig(
    ...
    providers=[MagicLinksProvider()],
    token_adapter=adapter.token,
    email_transport=SMTPTransport(...),
    base_url="https://your-app.com",
))
pip install "sreekarnv-fastauth[standard,email]"

See the Magic Links guide and example app.


Passkeys (WebAuthn)

Add Touch ID, Face ID, and Windows Hello sign-in with one extra import:

from fastauth.providers.passkey import PasskeyProvider
from fastauth.session_backends.memory import MemorySessionBackend

auth = FastAuth(FastAuthConfig(
    ...
    providers=[
        CredentialsProvider(),
        PasskeyProvider(rp_id="example.com", rp_name="My App", origin="https://example.com"),
    ],
    passkey_adapter=adapter.passkey,
    passkey_state_store=MemorySessionBackend(),
))
pip install "sreekarnv-fastauth[standard,webauthn]"

See the Passkeys guide and example app.


Custom Email Templates

Drop Jinja2 templates into any directory to override FastAuth's built-in emails. Only the files you provide are replaced — everything else falls back to the defaults automatically.

from pathlib import Path

auth = FastAuth(FastAuthConfig(
    ...
    email_template_dir=Path("my_templates/"),
))
Template file Sent when Variables
welcome.jinja2 User registers name
verification.jinja2 Email verification name, url, expires_in_minutes
password_reset.jinja2 Password reset name, url, expires_in_minutes
email_change.jinja2 Email change name, new_email, url, expires_in_minutes
magic_link_login.jinja2 Magic link sign-in name, url

See the example app.


Documentation

Full documentation at sreekarnv.github.io/fastauth


License

MIT License - see LICENSE for details.

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

sreekarnv_fastauth-0.5.0.tar.gz (36.8 kB view details)

Uploaded Source

Built Distribution

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

sreekarnv_fastauth-0.5.0-py3-none-any.whl (58.3 kB view details)

Uploaded Python 3

File details

Details for the file sreekarnv_fastauth-0.5.0.tar.gz.

File metadata

  • Download URL: sreekarnv_fastauth-0.5.0.tar.gz
  • Upload date:
  • Size: 36.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for sreekarnv_fastauth-0.5.0.tar.gz
Algorithm Hash digest
SHA256 3f76f9f30196c50da70b563b1a26875a902bdfa1d2c2192c42dd0470ed5e381d
MD5 5ceb25a7bc9fd57999b7df53e610b3c1
BLAKE2b-256 dee9697b0af62960ed88376be3d6bee7aa5fade64689c8f67bf40c117d480c09

See more details on using hashes here.

File details

Details for the file sreekarnv_fastauth-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: sreekarnv_fastauth-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 58.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for sreekarnv_fastauth-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fbe87a50e6ff9f7bf50bd58b3c87afc9f3916de9db475fc3325154433090c0cd
MD5 c20120f02faad07c9bb165f506bdcee4
BLAKE2b-256 c0edc098135ecc8e2fbcb04012edd34e24747d08a64de30e4507ee7215f95af4

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