Skip to content

Adapters

fastauth.adapters.sqlalchemy.SQLAlchemyAdapter

Factory that creates all FastAuth sub-adapters from a single SQLAlchemy engine.

Pass either a connection URL string or a pre-created :class:sqlalchemy.ext.asyncio.AsyncEngine. All sub-adapters share the same engine and session factory so you don't have to manage multiple connections.

Supported databases (via async drivers):

Database Driver URL prefix
SQLite aiosqlite sqlite+aiosqlite:///...
PostgreSQL asyncpg postgresql+asyncpg://...
MySQL aiomysql mysql+aiomysql://...
Example
from fastauth.adapters.sqlalchemy import SQLAlchemyAdapter

# SQLite for local development
adapter = SQLAlchemyAdapter(engine_url="sqlite+aiosqlite:///./auth.db")

config = FastAuthConfig(
    secret="...",
    providers=[...],
    adapter=adapter.user,
    token_adapter=adapter.token,
    oauth_adapter=adapter.oauth,
)

# Create tables on startup
@asynccontextmanager
async def lifespan(app):
    await adapter.create_tables()
    yield

user property

user: SQLAlchemyUserAdapter

Return a :class:~fastauth.adapters.sqlalchemy.user.SQLAlchemyUserAdapter.

token property

token: SQLAlchemyTokenAdapter

Return a :class:~fastauth.adapters.sqlalchemy.token.SQLAlchemyTokenAdapter.

session property

session: SQLAlchemySessionAdapter

Return a :class: ~fastauth.adapters.sqlalchemy.session.SQLAlchemySessionAdapter.

role property

role: SQLAlchemyRoleAdapter

Return a :class:~fastauth.adapters.sqlalchemy.rbac.SQLAlchemyRoleAdapter.

oauth property

oauth: SQLAlchemyOAuthAccountAdapter

Returns a :class: ~fastauth.adapters.sqlalchemy.oauth.SQLAlchemyOAuthAccountAdapter.

passkey property

passkey: SQLAlchemyPasskeyAdapter

Return a :class: ~fastauth.adapters.sqlalchemy.passkey.SQLAlchemyPasskeyAdapter.

__init__

__init__(
    engine_url: str | None = None,
    engine: AsyncEngine | None = None,
) -> None

Initialize the adapter with a connection URL or an existing engine.

Parameters:

Name Type Description Default
engine_url str | None

SQLAlchemy async connection string.

None
engine AsyncEngine | None

A pre-created :class:~sqlalchemy.ext.asyncio.AsyncEngine.

None

Raises:

Type Description
ValueError

If neither engine_url nor engine is provided.

create_tables async

create_tables() -> None

Create all FastAuth database tables if they do not already exist.

Safe to call on every startup — uses CREATE TABLE IF NOT EXISTS semantics via SQLAlchemy's create_all.

drop_tables async

drop_tables() -> None

Drop all FastAuth database tables.

Intended for testing and local teardown only. Do not call in production — all user data will be permanently deleted.


fastauth.adapters.memory.MemoryUserAdapter

Bases: UserAdapter

In-memory user adapter for testing.


fastauth.adapters.memory.MemoryTokenAdapter

In-memory one-time token adapter for testing.


fastauth.adapters.memory.MemorySessionAdapter

In-memory session adapter for testing.


fastauth.adapters.memory.MemoryRoleAdapter

In-memory RBAC adapter for testing.


fastauth.adapters.memory.MemoryOAuthAccountAdapter

In-memory OAuth account adapter for testing.