Crate torii_axum

Crate torii_axum 

Source
Expand description

§Torii Axum Integration

This crate provides Axum routes and middleware for the Torii authentication framework. It offers a simple way to add authentication to your Axum application with support for multiple authentication methods.

§Features

  • Core Authentication: Session management, user info, health checks
  • Password Authentication (feature = “password”): Registration, login, password changes
  • Magic Link Authentication (feature = “magic-link”): Passwordless email authentication
  • OAuth (feature = “oauth”): Coming soon
  • Passkey (feature = “passkey”): Coming soon

§Example Usage

use std::sync::Arc;
use axum::{Router, routing::get};
use torii::{Torii, SeaORMRepositoryProvider};
use torii_axum::{routes, HasTorii, auth_middleware, CookieConfig};

// Define your application state with a torii field
#[derive(Clone)]
struct AppState {
    torii: Arc<Torii<SeaORMRepositoryProvider>>,
    // Add other state fields as needed
    // database: Arc<sqlx::PgPool>,
}

// Implement HasTorii for your state
impl HasTorii<SeaORMRepositoryProvider> for AppState {
    fn torii(&self) -> &Arc<Torii<SeaORMRepositoryProvider>> {
        &self.torii
    }
}

#[tokio::main]
async fn main() {
    // Set up Torii with your storage backend
    let repositories = Arc::new(SeaORMRepositoryProvider::new(pool));
    let torii = Arc::new(Torii::new(repositories));

    // Create your application state
    let state = AppState { torii: torii.clone() };

    // Create auth routes with custom cookie configuration
    let auth_routes = routes(torii)
        .with_cookie_config(CookieConfig::development())
        .build();

    // Create your application router
    let app = Router::new()
        .nest("/auth", auth_routes)
        .route("/protected", get(protected_handler))
        .with_state(state.clone())
        .layer(axum::middleware::from_fn_with_state(
            state,
            auth_middleware::<AppState, SeaORMRepositoryProvider>
        ));

    // Run your server
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

async fn protected_handler() -> &'static str {
    "This route requires authentication!"
}

Structs§

AuthResponse
AuthRouterBuilder
Builder for configuring authentication routes
AuthUser
ChangePasswordRequest
ConnectionInfo
CookieConfig
HealthResponse
LinkConfig
Configuration for verification links sent via email.
LoginRequest
MagicLinkRequest
MagicLinkResponse
MessageResponse
OptionalAuthUser
PasswordResetRequest
PasswordResetResponse
RegisterRequest
ResetPasswordRequest
SessionResponse
SessionTokenFromBearer
SessionTokenFromCookie
SessionTokenFromRequest
UserResponse
VerifyMagicTokenRequest
VerifyResetTokenResponse

Enums§

AuthError
CookieSameSite

Traits§

HasTorii
Trait for application states that contain a Torii instance.

Functions§

auth_middleware
create_router
require_auth
routes
Create authentication routes for your Axum application.

Type Aliases§

Result