Skip to main content

Builder

Struct Builder 

Source
pub struct Builder<D, U, P, S, B> { /* private fields */ }
Expand description

Collects configuration. .build() only exists once secret, base_url and database are set: the last three type parameters track that.

Implementations§

Source§

impl<D, U, P, S, B> Builder<D, U, P, S, B>

Source

pub fn secret(self, secret: Secret) -> Builder<D, U, P, Present, B>

The master secret (at least 32 bytes). See Secret::from_env.

Source

pub fn secrets( self, secrets: impl IntoIterator<Item = Secret>, ) -> Builder<D, U, P, Present, B>

Several secrets for rotation: the first signs, all verify.

Source

pub fn base_url(self, url: impl Into<String>) -> Builder<D, U, P, S, Present>

The public URL of the app, e.g. https://acme.dk.

Source

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

Source

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

Where auth routes live (default /api/auth).

Source

pub fn trusted_origins( self, origins: impl IntoIterator<Item = impl Into<String>>, ) -> Self

Origins allowed to make cookie-carrying requests and to be redirect targets.

Source

pub fn email_password(self, config: EmailPassword) -> Self

Source

pub fn email_verification(self, config: EmailVerification) -> Self

Source

pub fn session(self, config: SessionConfig) -> Self

Source

pub fn cookies(self, config: CookieConfig) -> Self

Source

pub fn rate_limit(self, config: RateLimit) -> Self

Source

pub fn client_ip(self, source: ClientIp) -> Self

Source

pub fn ids(self, strategy: IdStrategy) -> Self

How ids are generated and stored; IdStrategy::Ulid unless set.

Apps upgrading a Postgres database created by fastauth 0.1 (uuid id columns) keep it working with .ids(IdStrategy::UuidV7).

Source

pub fn mailer(self, mailer: impl Mailer) -> Self

Source

pub fn secondary_storage(self, storage: impl SecondaryStorage) -> Self

A key-value store every instance of the app shares, such as Redis (fastauth_redis::RedisStorage). Rate limit counters live there, so a limit holds across all instances. Without it, each instance keeps its own counters in memory.

Each call is bounded by secondary_storage_timeout. A call that fails or times out falls back to this instance’s in-memory limiter, and after 5 such calls in a row the storage is skipped for 5 seconds before one request probes it again, so a dead store doesn’t slow every request down.

Source

pub fn secondary_storage_timeout(self, timeout: Duration) -> Self

How long a request waits for one secondary storage call before its rate limit falls back to this instance’s memory. Default: 100 ms (DEFAULT_STORAGE_TIMEOUT).

Source

pub fn multi_instance(self, multi_instance: bool) -> Self

Says the app runs as several instances (processes, containers, machines) behind a load balancer. .build() then warns when rate limiting is on without secondary_storage, since each instance would allow the full limit.

Source

pub fn allow_insecure_config(self, allow: bool) -> Self

Builds a configuration that .build() refuses for production, logging each problem as a warning instead: an http:// base_url on a host other than localhost, or a current (first) secret with too little entropy. A weak retired secret is only ever logged, so rotating off one needs no opt-out. For local development, CI and staging setups on a private network only. A localhost base_url gets the same leniency without it.

Source

pub fn column_naming(self, naming: ColumnNaming) -> Self

How column names are stored in the database, for every table: core’s, the plugins’ and the app’s fields. ColumnNaming::CamelCase stores email_verified as emailVerified, as Better Auth does. Code keeps using the declared names (User::EMAIL_VERIFIED, #[derive(Fields)] field names); only the SQL changes. Default: ColumnNaming::SnakeCase, the declared names.

Source

pub fn rename_column( self, table: impl Into<String>, column: impl Into<String>, to: impl Into<String>, ) -> Self

Stores table.column (declared names) as to, over the naming strategy: .rename_column("account", "password_hash", "password") keeps the password hash in Better Auth’s password column. .build() fails on a column the schema doesn’t have.

Source

pub fn bytes_as_hex_text( self, table: impl Into<String>, column: impl Into<String>, ) -> Self

Stores the byte column table.column as lowercase hex text, for a text column another app created, such as Better Auth’s account.accessToken. The values stay what they are (access_token is still encrypted); only their encoding changes. Text that isn’t hex, which only the other app writes, reads as its UTF-8 bytes.

Source

pub fn session_tokens(self, tokens: SessionTokens) -> Self

How session tokens are stored: SessionTokens::Hashed (the default) or SessionTokens::Raw, the token itself in session.token, as Better Auth stores it. Raw tokens let both apps resolve each other’s sessions while they share a database, and give up the protection hashing gives a leaked table; see docs/concepts/03-security.md.

Source

pub fn only_owned_accounts(self, on: bool) -> Self

Leaves account rows of providers fastauth doesn’t own alone: it never reads them as a sign-in method, lists, changes, unlinks or deletes them, and refuses to delete a user that has one (409 USER_HAS_FOREIGN_ACCOUNTS, since the foreign key would delete it too). fastauth owns credential and the providers plugins register (each social provider). For a database another app writes accounts to, such as the oidc rows of an SSO integration. Off by default: fastauth manages every row.

Source

pub fn store_oauth_tokens_in_plaintext(self, on: bool) -> Self

Stores the OAuth access and refresh tokens providers return as plain text instead of encrypting them with the secret, as Better Auth does unless its account.encryptOAuthTokens is on. For a database a Better Auth app shares: its /get-access-token, and app code reading account.accessToken, then get the provider’s token after a sign-in through fastauth, not fastauth’s ciphertext. A token column stored as text (bytes_as_hex_text, which BetterAuthCompat sets) then holds the token itself rather than hex (BytesText::Utf8). The id token is stored as it is either way.

Rows already stored aren’t rewritten; Account::read_access_token reads both kinds. Anyone who can read the table, a backup or a replica can call the provider as the user, so turn it on only for the migration window; BetterAuthCompat doesn’t. Off by default: tokens are encrypted.

Source

pub fn migrations(self, mode: Migrations) -> Self

Whether auth.migrator() may change the database. Migrations::ReviewOnly makes run and apply (and fastauth migrate) refuse, for a schema another app’s migrations own, and fastauth generate write each step as its own migration to review. Default: Migrations::Apply.

Source

pub fn better_auth_compat(self, compat: BetterAuthCompat) -> Self

Shares sessions and passwords with a Better Auth app on the same database: Better Auth’s cookie format and secret, its session refresh and its scrypt password hashes. Applied at .build(), over the matching cookie, session and email-and-password settings. See BetterAuthCompat.

Source

pub fn user_fields<U2: Fields>(self) -> Builder<D, U2, P, S, B>

The app’s own columns on user.

Source

pub fn plugin<Pl: Plugin, I>(self, plugin: Pl) -> Builder<D, U, P::Out, S, B>
where P: Append<Pl>, Pl::Requires: Satisfied<P, I>,

Registers a plugin. Its Requires must already be registered.

Source§

impl<U, P, S, B> Builder<NoDatabase, U, P, S, B>

Source

pub fn database<D: Adapter>(self, db: D) -> Builder<D, U, P, S, B>

Source§

impl<D: Adapter, U, P, S, B> Builder<D, U, P, S, B>

Source

pub fn configure( self, init: impl FnOnce(&mut PluginBuilder<'_, D>) + Send + 'static, ) -> Self

Registers app-level hooks, gates, resolvers or endpoints, like a plugin’s init.

Source

pub fn on<Ev, F, Fut>(self, handler: F) -> Self
where Ev: Event, F: Fn(Ev, Ctx<D>) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<(), AuthError>> + Send + 'static,

Runs handler after every endpoint that emits Ev.

Source§

impl<D: Adapter, U: Fields, P: PluginSet> Builder<D, U, P, Present, Present>

Source

pub fn build(self) -> Result<Auth<D, U, P>, BuildError>

Trait Implementations§

Source§

impl<D, U, P, S, B> Debug for Builder<D, U, P, S, B>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<D, U, P, S, B> !RefUnwindSafe for Builder<D, U, P, S, B>

§

impl<D, U, P, S, B> !Sync for Builder<D, U, P, S, B>

§

impl<D, U, P, S, B> !UnwindSafe for Builder<D, U, P, S, B>

§

impl<D, U, P, S, B> Forget for Builder<D, U, P, S, B>

§

impl<D, U, P, S, B> Freeze for Builder<D, U, P, S, B>
where D: Freeze, P: Freeze, Vec<Box<dyn FnOnce(&mut PluginBuilder<'_, D>) + Send>>: Freeze, PhantomData<fn() -> (U, S, B)>: Freeze,

§

impl<D, U, P, S, B> Move for Builder<D, U, P, S, B>

§

impl<D, U, P, S, B> Send for Builder<D, U, P, S, B>
where P: Send, D: Send, Vec<Box<dyn FnOnce(&mut PluginBuilder<'_, D>) + Send>>: Send, PhantomData<fn() -> (U, S, B)>: Send,

§

impl<D, U, P, S, B> Unpin for Builder<D, U, P, S, B>
where P: Unpin, D: Unpin, Vec<Box<dyn FnOnce(&mut PluginBuilder<'_, D>) + Send>>: Unpin, PhantomData<fn() -> (U, S, B)>: Unpin,

§

impl<D, U, P, S, B> UnsafeUnpin for Builder<D, U, P, S, B>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more