Skip to main content

modo_auth/
lib.rs

1//! Session-based authentication and Argon2id password hashing for modo applications.
2//!
3//! # Overview
4//!
5//! `modo-auth` provides three building blocks:
6//!
7//! - **[`UserProvider`]** — implement this trait on your user repository to look up users by ID.
8//! - **[`Auth<U>`] / [`OptionalAuth<U>`]** — axum extractors that resolve the current user from
9//!   the session and the registered [`UserProviderService<U>`].
10//! - **[`PasswordHasher`]** — Argon2id hashing service configured via [`PasswordConfig`].
11//!
12//! An optional **`templates`** feature adds `UserContextLayer`, a Tower middleware that injects
13//! the authenticated user into the minijinja template context under the key `"user"`.
14//!
15//! # Quick start
16//!
17//! ```rust,ignore
18//! use modo_auth::{UserProvider, UserProviderService, Auth, PasswordHasher, PasswordConfig};
19//!
20//! struct UserRepo { /* db pool */ }
21//!
22//! impl UserProvider for UserRepo {
23//!     type User = MyUser;
24//!
25//!     async fn find_by_id(&self, id: &str) -> Result<Option<MyUser>, modo::Error> {
26//!         // load from DB
27//!         todo!()
28//!     }
29//! }
30//!
31//! #[modo::main]
32//! async fn main(app: modo::app::AppBuilder, config: Config) -> Result<(), Box<dyn std::error::Error>> {
33//!     let repo = UserRepo { /* ... */ };
34//!     let hasher = PasswordHasher::default();
35//!
36//!     app.service(UserProviderService::new(repo))
37//!        .service(hasher)
38//!        .run()
39//!        .await
40//! }
41//! ```
42
43pub(crate) mod cache;
44#[cfg(feature = "templates")]
45pub mod context_layer;
46pub mod extractor;
47pub mod password;
48pub mod provider;
49
50pub use extractor::{Auth, OptionalAuth};
51pub use password::{PasswordConfig, PasswordHasher};
52pub use provider::{UserProvider, UserProviderService};
53
54#[cfg(feature = "templates")]
55pub use context_layer::UserContextLayer;