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 ;
use EmailPasswordPlugin;
use MemoryDatabaseAdapter;
async
Axum Integration
use ;
use EmailPasswordPlugin;
use MemoryDatabaseAdapter;
use AxumIntegration;
use Arc;
async
๐๏ธ 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 registrationPOST /sign-in- User loginGET /health- Health check
Feature Flags
[]
= []
= ["dep:axum", "dep:tower", "dep:tower-http"]
= ["dep:sqlx"]
= ["dep:redis"]
๐งช Running Examples
Basic Usage Example
# Run basic example (in-memory database)
PostgreSQL Example
# Run PostgreSQL example
Axum Web Server Example
# Run Axum web server with interactive demo
Then visit:
- http://localhost:8080 - Interactive web demo with sign-up/sign-in forms
- http://localhost:8080/auth/health - Health check endpoint
- http://localhost:8080/api/public - Public API endpoint
- http://localhost:8080/api/protected - Protected API endpoint (requires authentication)
Testing Compilation
# Test basic compilation
# Test PostgreSQL features
# Test Axum features
๐ ๏ธ 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 ๐