Auth for Rust, checked by the compiler.
Sign-in, sessions and two-factor for axum and sqlx, in the shape of Better Auth.
cargo add fastauth --features oauth,two-factorpub type AppAuth = Auth<Sqlx<Postgres>, Profile, plugins![Social, TwoFactor]>;#[derive(Fields, Clone, Debug, Serialize)] pub struct Profile { #[field(default = "free")] pub plan: String,}pub fn auth(pool: PgPool) -> Result<AppAuth, BuildError> { FastAuth:: builder() .secret(Secret:: from_env("FASTAUTH_SECRET" )?) .base_url("http://localhost:3000" ) .database(Sqlx:: new(pool)) .email_password(EmailPassword:: default()) .user_fields::< Profile>() .plugin(Social:: new(). provider(Github:: from_env()?)) .plugin(TwoFactor:: new(). issuer("Acme" )) .build() }A missing plugin is a compile error.
pub type AppAuth = Auth<Sqlx<Postgres>, Profile, plugins![Social]>;async fn me(session: Session<AppAuth>) -> Json<Me> { Json(Me { email: session. user. email. clone(), plan: session. user. fields. plan. clone(), two_factor: session. user. plugin::< TwoFactor, _>(). two_factor_enabled, })}cargo build
error[E0277]: plugin `TwoFactor` is not registered on this auth instance --> src/main.rs:38:34 | 38 | two_factor: session.user.plugin::<TwoFactor, _>().two_factor_enabled, | ^^^^^^ `TwoFactor` is not in this plugin list | = help: the trait `Has<TwoFactor, _>` is not implemented for `fastauth::Nil` = note: register it with `.plugin(...)` on the builder, before any plugin that requires it, and list it in your `plugins![..]` aliasChecked by rustc
- Reading a plugin's state
- Calling its server API
- Columns it adds to user
- Plugins it depends on
Better Auth's TypeScript client works unchanged.
- Tested against
- better-auth 1.7.28
- Clients
- React, Vue, Svelte
- Same as Better Auth
- Paths, JSON shapes, error codes
- Endpoints
- Core and two-factor
import { createAuthClient } from "better-auth/client"; // or better-auth/react, /vue, /svelteimport { twoFactorClient } from "better-auth/client/plugins";export const authClient = createAuthClient({ baseURL: "http://localhost:3000", // fastauth's base_url; the client adds /api/auth plugins: [twoFactorClient({ onTwoFactorRedirect: () => (location. href = "/2fa") })], });What's in 0.1
- fastauth
- The crate apps depend on: re-exports everything below behind features
- fastauth-core
- Builder, compile-checked plugin list, sessions, crypto, routing, email and password, verification, reset
- fastauth-axum
- auth.router(), Session / FreshSession extractors, the session layer
- fastauth-sqlx
- Postgres and SQLite adapter, migrations, shared transactions (.in_tx)
- fastauth-macros
- #[derive(Fields, Model, AuthError)]
- fastauth-oauth
- The Social plugin: GitHub and Google
- fastauth-plugin-two-factor
- TOTP, email codes, backup codes, trusted devices
- fastauth-cli
- fastauth secret; schema and migration commands run from your app via fastauth::cli::run
- On main, not released yet
- Organization, admin and API key plugins
- Not built yet
- Passkey, magic link and JWT plugins, actix-web, Redis and MySQL