Skip to main content

AuthConfig

Struct AuthConfig 

Source
pub struct AuthConfig {
    pub secret: String,
    pub app_name: String,
    pub base_url: String,
    pub base_path: String,
    pub trusted_origins: Vec<String>,
    pub disabled_paths: Vec<String>,
    pub session: SessionConfig,
    pub jwt: JwtConfig,
    pub password: PasswordConfig,
    pub account: AccountConfig,
    pub email_provider: Option<Arc<dyn EmailProvider>>,
    pub advanced: AdvancedConfig,
}
Expand description

Main configuration for BetterAuth

Fields§

§secret: String

Secret key for signing tokens and sessions

§app_name: String

Application name, used for cookie prefixes, email templates, etc.

Defaults to "Better Auth".

§base_url: String

Base URL for the authentication service (e.g. "http://localhost:3000").

§base_path: String

Base path where the auth routes are mounted.

All routes handled by BetterAuth will be prefixed with this path. For example, with the default "/api/auth", the sign-in route becomes "/api/auth/sign-in/email".

Defaults to "/api/auth".

§trusted_origins: Vec<String>

Origins that are trusted for CSRF and other cross-origin checks.

Supports glob patterns (e.g. "https://*.example.com"). These are shared across all middleware that needs origin validation (CSRF, CORS, etc.).

§disabled_paths: Vec<String>

Paths that should be disabled (skipped) by the router.

Any request whose path matches an entry in this list will receive a 404 response, even if a handler is registered for it.

§session: SessionConfig

Session configuration

§jwt: JwtConfig

JWT configuration

§password: PasswordConfig

Password configuration

§account: AccountConfig

Account configuration (linking, token encryption, etc.)

§email_provider: Option<Arc<dyn EmailProvider>>

Email provider for sending emails (verification, password reset, etc.)

§advanced: AdvancedConfig

Advanced configuration options

Implementations§

Source§

impl AuthConfig

Source

pub fn new(secret: impl Into<String>) -> Self

Source

pub fn app_name(self, name: impl Into<String>) -> Self

Set the application name.

Source

pub fn base_url(self, url: impl Into<String>) -> Self

Set the base URL (e.g. "https://myapp.com").

Source

pub fn account(self, account: AccountConfig) -> Self

Source

pub fn base_path(self, path: impl Into<String>) -> Self

Set the base path where auth routes are mounted.

Source

pub fn trusted_origin(self, origin: impl Into<String>) -> Self

Add a trusted origin. Supports glob patterns (e.g. "https://*.example.com").

Source

pub fn trusted_origins(self, origins: Vec<String>) -> Self

Set all trusted origins at once.

Source

pub fn disabled_path(self, path: impl Into<String>) -> Self

Add a path to the disabled paths list.

Source

pub fn disabled_paths(self, paths: Vec<String>) -> Self

Set all disabled paths at once.

Source

pub fn session_expires_in(self, duration: Duration) -> Self

Set the session expiration duration.

Source

pub fn session_update_age(self, duration: Duration) -> Self

Source

pub fn disable_session_refresh(self, disabled: bool) -> Self

Source

pub fn session_fresh_age(self, duration: Duration) -> Self

Set the cookie cache configuration for sessions.

Source

pub fn jwt_expires_in(self, duration: Duration) -> Self

Set the JWT expiration duration.

Source

pub fn password_min_length(self, length: usize) -> Self

Set the minimum password length.

Source

pub fn advanced(self, advanced: AdvancedConfig) -> Self

Source

pub fn cookie_prefix(self, prefix: impl Into<String>) -> Self

Source

pub fn disable_csrf_check(self, disabled: bool) -> Self

Source

pub fn cross_sub_domain_cookies(self, domain: impl Into<String>) -> Self

Source

pub fn is_origin_trusted(&self, origin: &str) -> bool

Check whether a given origin is trusted.

An origin is trusted if it matches:

  1. The origin extracted from base_url, or
  2. Any pattern in trusted_origins (after extracting the origin portion from the pattern).

Glob patterns are supported — * matches any characters except /, ** matches any characters including /.

Source

pub fn is_path_disabled(&self, path: &str) -> bool

Check whether a given path is disabled.

Source

pub fn validate(&self) -> Result<(), AuthError>

Trait Implementations§

Source§

impl Clone for AuthConfig

Source§

fn clone(&self) -> AuthConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for AuthConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.