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
¶
Return a :class:~fastauth.adapters.sqlalchemy.user.SQLAlchemyUserAdapter.
token
property
¶
Return a :class:~fastauth.adapters.sqlalchemy.token.SQLAlchemyTokenAdapter.
session
property
¶
Return a :class: ~fastauth.adapters.sqlalchemy.session.SQLAlchemySessionAdapter.
role
property
¶
Return a :class:~fastauth.adapters.sqlalchemy.rbac.SQLAlchemyRoleAdapter.
oauth
property
¶
Returns a :class: ~fastauth.adapters.sqlalchemy.oauth.SQLAlchemyOAuthAccountAdapter.
passkey
property
¶
Return a :class: ~fastauth.adapters.sqlalchemy.passkey.SQLAlchemyPasskeyAdapter.
__init__
¶
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: |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither engine_url nor engine is provided. |
create_tables
async
¶
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 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
¶
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.