better-auth

Crates.iobetter-auth
lib.rsbetter-auth
version0.0.1-alpha.2
created_at2025-07-25 02:44:41.602752+00
updated_at2025-08-14 06:50:48.223532+00
descriptionThe most comprehensive authentication framework for Rust
homepage
repositoryhttps://github.com/better-auth-rs/better-auth-rs
max_upload_size
id1767134
size650,752
AprilNEA (AprilNEA)

documentation

README

Better Auth - Rust ๐Ÿ”

A Rust authentication framework inspired by Better-Auth, providing a plugin-based architecture and type-safe authentication solutions.

โœจ Features

  • ๐Ÿ”Œ Plugin Architecture - Easily extend and customize authentication flows
  • ๐Ÿ›ก๏ธ Type Safety - Leverage Rust's type system to ensure code safety
  • โšก Async Support - Full support for asynchronous operations
  • ๐Ÿ—„๏ธ Database Agnostic - Support for multiple databases through adapter pattern
  • ๐ŸŒ Web Framework Integration - Support for Axum (extensible to other frameworks)
  • ๐Ÿ”‘ Multiple Authentication Methods - Email/password, OAuth, two-factor authentication, etc.

๐Ÿš€ Quick Start

Basic Usage

use better_auth::{BetterAuth, AuthConfig};
use better_auth::plugins::EmailPasswordPlugin;
use better_auth::adapters::MemoryDatabaseAdapter;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create configuration
    let config = AuthConfig::new("your-very-secure-secret-key-at-least-32-chars-long")
        .base_url("http://localhost:3000")
        .password_min_length(8);
    
    // Create authentication system
    let auth = BetterAuth::new(config)
        .database(MemoryDatabaseAdapter::new())
        .plugin(EmailPasswordPlugin::new().enable_signup(true))
        .build()
        .await?;
    
    println!("๐Ÿ” Authentication system ready!");
    println!("Registered plugins: {:?}", auth.plugin_names());
    
    Ok(())
}

Axum Integration

use better_auth::{BetterAuth, AuthConfig};
use better_auth::plugins::EmailPasswordPlugin;
use better_auth::adapters::MemoryDatabaseAdapter;
use better_auth::handlers::AxumIntegration;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = AuthConfig::new("your-secret-key");
    
    let auth = Arc::new(
        BetterAuth::new(config)
            .database(MemoryDatabaseAdapter::new())
            .plugin(EmailPasswordPlugin::new())
            .build()
            .await?
    );
    
    // Create Axum router
    let app = auth.axum_router();
    
    // Start server
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?;
    axum::serve(listener, app).await?;
    
    Ok(())
}

๐Ÿ—๏ธ Architecture Design

Core Components

  • BetterAuth - Main authentication instance
  • AuthPlugin - Plugin system abstraction
  • DatabaseAdapter - Database adapter abstraction
  • AuthConfig - Configuration management

Plugin System

  • EmailPasswordPlugin - Email/password authentication โœ…
  • OAuthPlugin - OAuth authentication ๐Ÿšง
  • TwoFactorPlugin - Two-factor authentication ๐Ÿšง

Database Adapters

  • MemoryDatabaseAdapter - In-memory database (development/testing) โœ…
  • SqlxAdapter - Full PostgreSQL support โœ…
    • Connection pool optimization
    • Automatic migration scripts
    • Type-safe mapping
    • Performance-optimized indexes

๐Ÿ“ Project Structure

better-auth/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ core/           # Core functionality
โ”‚   โ”‚   โ”œโ”€โ”€ auth.rs     # Main authentication logic
โ”‚   โ”‚   โ”œโ”€โ”€ config.rs   # Configuration management
โ”‚   โ”‚   โ”œโ”€โ”€ plugin.rs   # Plugin abstraction
โ”‚   โ”‚   โ””โ”€โ”€ session.rs  # Session management
โ”‚   โ”œโ”€โ”€ plugins/        # Authentication plugins
โ”‚   โ”‚   โ”œโ”€โ”€ email_password.rs
โ”‚   โ”‚   โ”œโ”€โ”€ oauth.rs
โ”‚   โ”‚   โ””โ”€โ”€ two_factor.rs
โ”‚   โ”œโ”€โ”€ adapters/       # Database and cache adapters
โ”‚   โ”‚   โ”œโ”€โ”€ database.rs
โ”‚   โ”‚   โ””โ”€โ”€ cache.rs
โ”‚   โ”œโ”€โ”€ handlers/       # Web framework integration
โ”‚   โ”‚   โ””โ”€โ”€ axum.rs
โ”‚   โ”œโ”€โ”€ error.rs        # Error types
โ”‚   โ””โ”€โ”€ types.rs        # Core type definitions
โ””โ”€โ”€ examples/
    โ”œโ”€โ”€ basic_usage.rs     # Basic authentication example
    โ”œโ”€โ”€ postgres_usage.rs  # PostgreSQL database example
    โ””โ”€โ”€ axum_server.rs     # Complete web server with demo UI

๐Ÿ”ง Available Features

Authentication Endpoints

  • POST /sign-up - User registration
  • POST /sign-in - User login
  • GET /health - Health check

Feature Flags

[features]
default = []
axum = ["dep:axum", "dep:tower", "dep:tower-http"]
sqlx-postgres = ["dep:sqlx"]
redis-cache = ["dep:redis"]

๐Ÿงช Running Examples

Basic Usage Example

# Run basic example (in-memory database)
cargo run --example basic_usage

PostgreSQL Example

# Run PostgreSQL example
export DATABASE_URL="postgresql://better_auth:password@localhost:5432/better_auth"
cargo run --example postgres_usage --features sqlx-postgres

Axum Web Server Example

# Run Axum web server with interactive demo
cargo run --example axum_server --features axum

Then visit:

Testing Compilation

# Test basic compilation
cargo check

# Test PostgreSQL features
cargo check --features sqlx-postgres

# Test Axum features
cargo check --features axum

๐Ÿ› ๏ธ Development Status

โœ… Completed

  • Core architecture design
  • Plugin system implementation
  • Email/password authentication
  • In-memory database adapter
  • Session management
  • Axum integration
  • Basic examples

โœ… Recently Completed

  • PostgreSQL Database Support - Complete SQLx adapter with connection pool optimization
  • Database Migration Scripts - Automated table structure creation
  • Type-Safe Mapping - Perfect integration between PostgreSQL and Rust types
  • Performance-Optimized Indexes - Database optimization for common queries

๐Ÿšง In Progress

  • OAuth plugin implementation
  • Two-factor authentication
  • Redis cache support
  • More web framework integrations

๐Ÿค Contributing

Contributions are welcome! Please check the project structure and existing implementations to understand how to add new features.

๐Ÿ“„ License

MIT License


Better Auth Rust - Build secure, scalable authentication systems ๐Ÿš€

Commit count: 0

cargo fmt