Skip to content

FastAuth

fastauth.app.FastAuth

Central FastAuth instance wires configuration, providers, and adapters together.

Pass a :class:~fastauth.config.FastAuthConfig to the constructor, then call :meth:mount to attach all authentication routes to a FastAPI application.

Example
from fastauth import FastAuth, FastAuthConfig
from fastauth.providers.credentials import CredentialsProvider
from fastauth.adapters.sqlalchemy import SQLAlchemyAdapter

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

config = FastAuthConfig(
    secret="change-me-in-production",
    providers=[CredentialsProvider()],
    adapter=adapter.user,
    token_adapter=adapter.token,
)

auth = FastAuth(config)

__init__

__init__(config: FastAuthConfig) -> None

Initialize FastAuth with the given configuration.

Parameters:

Name Type Description Default
config FastAuthConfig

A fully-populated :class:~fastauth.config.FastAuthConfig.

required

mount

mount(app: object) -> None

Mount all FastAuth routes onto a FastAPI application.

Attaches the authentication router at config.route_prefix (default /auth). Also mounts the /.well-known/jwks.json endpoint when a JWKS manager has been initialized.

Parameters:

Name Type Description Default
app object

A :class:fastapi.FastAPI instance.

required

Raises:

Type Description
AssertionError

If app is not a :class:fastapi.FastAPI instance.

MissingDependencyError

If the fastapi extra is not installed.

initialize_roles async

initialize_roles() -> None

Seed roles defined in config.roles into the role adapter.

Must be called inside the application lifespan startup handler when config.roles is set and a role_adapter has been assigned.

Example
@asynccontextmanager
async def lifespan(app: FastAPI):
    await auth.initialize_roles()
    yield

initialize_jwks async

initialize_jwks() -> None

Initialize the JWKS manager for RSA-based JWT signing.

Must be called inside the application lifespan startup handler when using RS256 / RS512 algorithms with jwks_enabled=True.

Example
from contextlib import asynccontextmanager
from fastapi import FastAPI

@asynccontextmanager
async def lifespan(app: FastAPI):
    await auth.initialize_jwks()
    yield

app = FastAPI(lifespan=lifespan)
auth.mount(app)  # mount registers routes based on config, not runtime state