Better Auth in Rust

Database

SeaORM-backed database integration for Better Auth.

Better Auth uses your application's SeaORM entities and DatabaseConnection for auth persistence through the seaorm2 feature on better-auth.

The supported public path is:

  1. Enable the seaorm2 feature on better-auth.
  2. Define auth entities in your app with #[derive(AuthEntity)] from better_auth::seaorm.
  3. Register them in an app schema with #[derive(AuthSchema)].
  4. Manage auth migrations in your own SeaORM migrator.
  5. Build a SeaOrmStore from the shared DatabaseConnection and pass it to BetterAuth::<YourSchema>::new(config).store(...).

Setup

use better_auth::{AuthConfig, AuthSchema, BetterAuth};
use better_auth::plugins::EmailPasswordPlugin;
use better_auth::seaorm::{AuthEntity, Database, DatabaseConnection, SeaOrmStore};
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let database = Database::connect("sqlite::memory:").await?;
    let auth_database: DatabaseConnection = database.clone();
    let config = AuthConfig::new("your-very-secure-secret-key-at-least-32-chars-long")
        .base_url("http://localhost:3000");
    let store = SeaOrmStore::<AppAuthSchema>::new(config.clone(), auth_database);

    let auth = BetterAuth::<AppAuthSchema>::new(
        config,
    )
    .store(store)
    .plugin(EmailPasswordPlugin::new().enable_signup(true))
    .build()
    .await?;

    // Keep using `database` for your app's own tables and queries.
    println!("plugins: {:?}", auth.plugin_names());
    Ok(())
}

Existing Databases

If your application already has a users table with extra fields or non-string primary keys, keep the same public integration story:

  1. Reuse your app-owned SeaORM entities.
  2. Implement the public AuthUser / SeaOrmUserModel traits manually instead of relying on #[derive(AuthEntity)].
  3. Keep sessions, accounts, and verifications as app-owned SeaORM entities in the same database.
  4. Run your own SeaORM migrations alongside the rest of the application schema.

The canonical worked example is examples/postgres_usage.rs, which demonstrates:

  • an existing app-owned schema
  • numeric user IDs
  • seeded legacy users
  • additional required app fields on the user table
  • Better Auth mounted on top of the same DatabaseConnection

Testing

Repo-local tests use SQLite through the same DatabaseConnection API and a bundled internal schema. The dual-server and client SDK compatibility checks still validate behavior against the TypeScript reference server.

Notes

  • Better Auth no longer owns a public SeaORM migration path. Auth and app tables should be migrated together by the host application.
  • MemoryCacheAdapter is still available for cache/rate-limit use cases. It is separate from database persistence.

On this page