Skip to content

Authentication and authorization ​

Authentication identifies the caller. Authorization decides which data that caller may use. Configure sign-in methods in the app's configure function (src/app.rs), then enforce access in your public queries, mutations, and actions.

Enable sign-in ​

rust
// src/app.rs
use rxferrix::RxFerrix;
use rxferrix::auth::AuthConfig;

#[rxferrix::app]
pub fn configure() -> RxFerrix {
    RxFerrix::new()
        .auth(AuthConfig::password()
            .magic_link()
            .redirect_url("http://localhost:5173"))
}

main runs it with app::configure().run(), and tests get the same configuration.

password() enables email/password accounts. magic_link() adds email-code/link sign-in. For email codes and links only, start from AuthConfig::passwordless() instead: the password endpoints then answer 404 not_enabled. The redirect URL is the frontend users return to after following a link or completing OAuth.

In development without SMTP, auth email codes and links appear in the backend log. For delivery, configure --smtp-url and --smtp-from, a custom mailer, or the dashboard's SMTP settings.

Set the backend's --public-url in production so email links and OAuth callbacks use its public address. Keep allowed frontend return URLs in redirect_url(...), --auth-redirect-url, or dashboard settings.

Sign in from a client ​

The core auth manager stores browser tokens in localStorage, refreshes them, and authenticates the client's WebSocket:

ts
import { getAuth } from "@rxferrix/client";

const auth = getAuth(client);
const result = await auth.signIn({ email, password });
if (result.mfaRequired) {
  await auth.verifyMfa(result.mfaToken, codeFromAuthenticator);
}

client is the app's existing RxFerrixClient. Call auth.handleRedirectCallback() on the page receiving OAuth or email redirects, and handle a possible MFA challenge there too.

TaskClient method
Create a password accountsignUp({ email, password })
Sign insignIn({ email, password })
Send and verify an email coderequestMagicLink(...), then verifyCode(...)
Start OAuthsignInWithOAuth("github")
Sign outsignOut()
Change a password while signed inchangePassword(currentPassword, newPassword)
Recover a passwordrequestPasswordReset(...), then resetPassword(...)

React exposes state through useRxFerrixAuth(), actions through useAuthActions(), and the user's document through useCurrentUser(). Svelte combines state and actions in useAuth() and also provides useCurrentUser(). See Clients.

Check access to each document ​

This example stores notes owned by an app user:

rust
use rxferrix::auth::User;
use rxferrix::prelude::*;

#[table(name = "notes")]
#[index(by_owner = [owner])]
pub struct Note {
    pub owner: Id<User>,
    pub text: String,
}

#[query]
pub async fn list(ctx: &QueryCtx, SignedIn(owner): SignedIn) -> Result<Vec<Doc<Note>>> {
    ctx.db.query::<Note>().by(Note::by_owner, owner).take(100).await
}

#[mutation]
pub async fn add(ctx: &MutationCtx, SignedIn(owner): SignedIn, text: String) -> Result<Id<Note>> {
    ctx.db.insert(Note { owner, text }).await
}

#[mutation]
pub async fn remove(ctx: &MutationCtx, SignedIn(owner): SignedIn, id: Id<Note>) -> Result<()> {
    let note = ctx.db.get(id).await?.or_user("not_found", "Note not found")?;
    ensure_user!(note.owner == owner, "forbidden", "You cannot delete this note");
    ctx.db.delete(id).await
}

A SignedIn parameter is the signed-in user's Id<User>. It is not an argument: clients, ctx.run_mutation::<add>(text) and TestApp calls pass only text, and the generated TypeScript leaves it out. A signed-out caller gets unauthenticated before the function runs, before its arguments are even read. It is the same check as let owner = ctx.auth.require_user()?; at the top of the function, which still works. Where signing in is optional, read ctx.auth.user_id(), an Option, instead. Write the type SignedIn or rxferrix::auth::SignedIn: the macros recognize it by those spellings. The list selects that user's rows; deletion checks ownership explicitly. Knowing an ID never grants access by itself.

Scheduled jobs and crons run with no identity, so a function taking SignedIn fails there; pass the user as an argument instead (see scheduler). A #[workflow] can't take one.

Authorization reads are tracked along with a subscribed query's other reads, so changing access data can invalidate the result. Use internal functions for operations that only trusted server code should call.

Apply an app-wide guard ​

A guard can enforce a common requirement before every client call to your public functions:

rust
rxferrix::RxFerrix::new()
    .guard(|ctx, _function| async move {
        ctx.auth.require_user()?;
        Ok(())
    })
    .run()

The callback can also read access data through ctx.db or exempt selected functions with function.is::<module::function>(). Its reads use the call's snapshot/transaction and participate in query invalidation.

Guards apply to client calls to app public functions. Internal functions, built-in _system/* functions, scheduled jobs, action-to-function calls, trusted App calls, and the admin function runner bypass them. Keep document-specific checks in the functions that need them. In a #[rxferrix::test], the guard from the app's #[rxferrix::app] function applies (Testing); a hand-built TestApp sets AppOptions::guard with Guard::new(...). TestApp::as_user exercises client access checks.

Add OAuth ​

Add a provider to the auth configuration, for example .oauth(rxferrix::auth::GitHub::from_env()).

Presets are GitHub, Google, Discord, and Apple; Oidc supports other OpenID Connect issuers. from_env() reads RXFERRIX_AUTH_<PROVIDER>_CLIENT_ID and related variables. Oidc::from_env(id) also reads RXFERRIX_AUTH_<ID>_ISSUER. Providers can also be configured through the dashboard.

Register the provider callback as https://your-backend/api/auth/oauth/<provider>/callback. Accounts are linked by email only when the provider verifies that address.

For Apple, configure the Services ID, team ID, key ID, and .p8 private key (RXFERRIX_AUTH_APPLE_PRIVATE_KEY or _PRIVATE_KEY_FILE). RxFerrix creates and rotates the client secret. Apple requires an HTTPS public callback and uses form_post; the name supplied on the first sign-in is retained. Store the private key outside source control or in encrypted dashboard settings.

Use an external identity provider ​

To accept JWTs from Clerk, Auth0, Supabase, or another issuer:

rust
use rxferrix::auth::{AuthConfig, JwtProvider};

let auth = AuthConfig::default().jwt(
    JwtProvider::new("https://tenant.auth0.com/")
        .audience("https://api.example.com")
);

Pass this configuration to .auth(auth). RxFerrix discovers the issuer's JWKS and validates signatures and claims. Supported algorithms are RS256, ES256, and EdDSA. Each new subject gets a _users row, so app code continues using Id<User>.

Give the core client a token getter from your provider:

ts
client.setAuth(({ forceRefresh }) =>
  clerk.session!.getToken({ skipCache: forceRefresh })
);

The client refreshes before token expiry and after rejection. A getter that throws is retried with backoff. If your issuer does not use an audience claim, configure .any_audience() explicitly.

Add two-factor authentication ​

Users and administrators can enroll a TOTP authenticator without extra server infrastructure. Accounts with an email address must verify it first. .totp_issuer("My App") changes the name shown by authenticator apps.

ts
const { qrSvg, secret } = await auth.setupTotp({ password });
// Show the QR code or secret, then ask for the first code.
const { recoveryCodes } = await auth.confirmTotp(firstCode);
// Show the recovery codes once and ask the user to save them.

Every enabled sign-in method then returns an MFA challenge until a TOTP code or recovery code is verified. getMfaStatus(), disableMfa(code), and regenerateRecoveryCodes(code) manage enrollment. Five wrong codes lock the factor for five minutes; a code cannot be reused.

Administrators can reset a user's lost factor on Users. For an administrator who lost both the authenticator and recovery codes, stop the server and use the app's admin disable-2fa EMAIL command. Administrators manage their own enrollment on Account.

Control account creation and sign-in hooks ​

signups(false) disables sign-ups. allowed_domains(["example.com"]) and allowed_emails(["guest@example.net"]) restrict accepted addresses. With an allowlist, password accounts must verify their email before sign-in and OAuth must supply a verified address.

For invitations or another custom policy, use before_sign_up(|app, attempt| async move { ... }). The attempt contains the email, verification state, method, name, and optional frontend return URL. Return UserError to reject creation with a code the client can handle. The hook is a check; perform app-side invitation acceptance after sign-in because account creation can still fail after the check passes.

on_sign_in(|app, sign_in| async move { ... }) runs after a successful session-creating sign-in. It receives the user, session, method, and client information. Keep it short: the response waits for it. It can't refuse the sign-in and returns nothing, so it handles its own errors. To change app data, such as recording login history or accepting a pending invitation, call an internal mutation with app.mutation::<f>(args):

rust
AuthConfig::passwordless().on_sign_in(|app, sign_in| async move {
    if let Err(e) = app.mutation::<invites::accept_pending>(sign_in.user).await {
        eprintln!("accepting invites failed: {e:#}");
    }
})

app.mutation runs with no signed-in user, so pass sign_in.user as an argument rather than taking SignedIn. The hook runs on every sign-in, so the mutation must be safe to repeat. sign_in has no email address; read the user's document in the mutation if you need it.

Send email and customize templates ​

Actions can use the same configured mailer as auth:

rust
use rxferrix::auth::Email;
use rxferrix::prelude::*;

#[internal_action]
pub async fn send_confirmation(ctx: &ActionCtx, to: String, text: String) -> Result<()> {
    ctx.send_email(Email::new(to, "Your booking", text)).await
}

Use .html(html) for an HTML body. Without a mailer this returns email_not_configured. Schedule email actions from mutations so a rolled-back mutation does not send mail; use retries only with an appropriate duplicate-delivery strategy.

In Settings → Email templates, customize the subject and HTML for verification, password reset, and sign-in. Placeholders are {link}, {code}, {expires}, {app_name}, and {email}. A template must include {code} or {link}; values are HTML-escaped and a plain-text alternative is generated.

Sessions, settings, and user removal ​

Built-in access tokens last 15 minutes by default; refresh tokens rotate. Sign-out, session revocation, user deletion, or detected refresh-token reuse revokes affected sockets promptly. A rotated refresh token presented again revokes its session, except for 10 seconds after the rotation (refresh_reuse_grace): within that window the same refresh gets the same new refresh token again (with a new access token), so an answer lost on the network or two tabs refreshing at once do not end the session. The client asks again while a refresh is unanswered; an answer that stays lost for longer than the window still ends the session, and the user signs in again. External tokens have no local session row; deleting their provisioned user also revokes their sockets.

The sign-in endpoints are rate limited per client IP: 10 credential checks (sign-in, sign-up, code verification) at once, then 10 a minute; 5 emails (codes, links, resets) at once, then 2 a minute, also per address; 30 token refreshes at once, then 60 a minute. Password guesses are also limited per account, administrators' included, with the credential budget. IPv6 clients are counted by their /64. Over a limit the endpoint answers 429 rate_limited with Retry-After and { "retryAfterSecs": n } as the error's data. serve --dev turns these limits off so a script can sign in many test users; change them, also for development, with AuthConfig::rate_limits(Some(RateLimits { .. })), or turn them off with rate_limits(None). They are separate from the per-function rate limits set in the dashboard.

The data directory's auth_secret.key signs built-in sessions and encrypts secrets. Preserve it when moving or restoring the app.

Dashboard administrators can update providers, SMTP, redirects, sign-up settings, token lifetimes, and CORS without restarting. Values explicitly fixed in code, flags, or environment take precedence. Admin changes and impersonation sessions are audited.

Mutations can call ctx.users.delete(user), revoke_sessions(user), or revoke_session(session) in their transaction. These helpers do not check app permissions: protect the calling mutation. Revoking sessions does not ban a user who can sign in again; store and enforce a ban in your access policy.

For imported users, _credentials.passwordHash accepts bcrypt hashes ($2a$, $2b$, $2y$) as well as argon2id. A successful bcrypt sign-in upgrades the hash to argon2id. Use the offline import command for system-table imports.

See the auth API contract and auth endpoints for full signatures and wire formats.