Skip to content

Memory adapter

Six in-memory sub-adapters, one per protocol. Use only in tests and local demos — they store state in Python dicts in the process and do not survive restarts.

from fastauth.adapters.memory import (
    MemoryUserAdapter,
    MemoryTokenAdapter,
    MemorySessionAdapter,
    MemoryRoleAdapter,
    MemoryOAuthAccountAdapter,
    MemoryPasskeyAdapter,
)

config = FastAuthConfig(
    ...,
    providers=[CredentialsProvider()],
    adapter=MemoryUserAdapter(),
    token_adapter=MemoryTokenAdapter(),
    # oauth_adapter=MemoryOAuthAccountAdapter(),     # for OAuth tests
    # passkey_adapter=MemoryPasskeyAdapter(),        # for passkey tests
)
auth = FastAuth(config)
auth.role_adapter = MemoryRoleAdapter()              # for RBAC tests
auth.session_adapter = MemorySessionAdapter()         # for /auth/sessions tests

What each provides

Adapter Notes
MemoryUserAdapter Email-normalized lookup, soft-delete flips is_active=False, password hashes held in a separate dict.
MemoryTokenAdapter consume_token is atomic (pop-after-validate). Expired rows auto-prune on get_token and consume_token.
MemorySessionAdapter Implements all SessionAdapter methods including cleanup_expired.
MemoryRoleAdapter Roles and user-role assignments in dicts.
MemoryOAuthAccountAdapter Keyed by (provider, provider_account_id).
MemoryPasskeyAdapter Timestamps stored as ISO strings to match the PasskeyData shape.

Each constructor takes no args.

Why memory?

  • Tests — no DB setup, no pytest fixtures, fast.
  • CI smoke — verify路由 integrate without spinning up Postgres.

Why NOT memory in production

  • State is process-local; multi-worker deployments lose consistency.
  • Lost on every restart.
  • No persistence for password resets / verification tokens between deploys.

Use SQLAlchemy for production.

Pattern: tests with memory adapters

import pytest
from fastauth import FastAuth, FastAuthConfig
from fastauth.adapters.memory import MemoryUserAdapter, MemoryTokenAdapter
from fastauth.providers.credentials import CredentialsProvider
from fastapi import FastAPI
from fastapi.testclient import TestClient

@pytest.fixture
def app():
    config = FastAuthConfig(
        secret="x" * 32,
        providers=[CredentialsProvider()],
        adapter=MemoryUserAdapter(),
        token_adapter=MemoryTokenAdapter(),
        base_url="http://testserver",
    )
    auth = FastAuth(config)
    app = FastAPI()
    auth.mount(app)
    return app

def test_register/app(app):
    client = TestClient(app)
    resp = client.post("/auth/register", json={"email":"a@b.com","password":"s3cur3!"})
    assert resp.status_code == 201
    assert "access_token" in resp.json()

See Testing guide for full coverage patterns.