Better Auth in Rust

Email & Password

Sign up and sign in with email, password, and optional username.

The EmailPasswordPlugin handles user registration and authentication via email/password or username/password.

Setup

These examples assume you've already defined AppAuthSchema. If you want a complete setup from entity definitions through Axum mounting, start with the Axum integration guide.

use better_auth::plugins::EmailPasswordPlugin;

let auth = BetterAuth::<AppAuthSchema>::new(config)
    .store(store)
    .plugin(
        EmailPasswordPlugin::new()
            .enable_signup(true)
            .username(true)
            .password_min_length(8)
            .require_email_verification(false)
    )
    .build()
    .await?;

Plugin Options

OptionTypeDefaultDescription
enable_signupbooltrueAllow new user registration
usernameboolfalseEnable username behavior; requires username and display_username fields
require_email_verificationboolfalseRequire verified email before sign-in
password_min_lengthusize8Minimum password length in UTF-16 code units

Username behavior is opt-in, like the upstream username plugin. Call .username(true) before build(). Without this option, username routes are absent and registration or update requests ignore username fields.

Sign Up

POST /sign-up/email
Content-Type: application/json

Request

{
  "name": "Alice",
  "email": "[email protected]",
  "password": "secure_password",
  "username": "alice",
  "displayUsername": "Alice"
}
FieldRequiredDescription
nameYesDisplay name
emailYesValid email address
passwordYesMust meet minimum length
usernameNoUnique username for username-based sign-in
displayUsernameNoCase-preserved display version of username
callbackURLNoRedirect URL after sign-up

Response

{
  "token": "session_abc123...",
  "user": {
    "id": "uuid",
    "name": "Alice",
    "email": "[email protected]",
    "emailVerified": false,
    "username": "alice",
    "displayUsername": "Alice",
    "createdAt": "2024-01-01T00:00:00Z",
    "updatedAt": "2024-01-01T00:00:00Z"
  }
}

Errors

StatusCondition
400Invalid email format, password too short, or USERNAME_IS_ALREADY_TAKEN
422USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL

Sign In with Email

POST /sign-in/email
Content-Type: application/json

Request

{
  "email": "[email protected]",
  "password": "secure_password"
}
FieldRequiredDescription
emailYesRegistered email address
passwordYesAccount password
callbackURLNoRedirect URL after sign-in
rememberMeNoDefaults to true. Set false for a 24-hour session and a browser-session cookie.

Response

{
  "redirect": false,
  "token": "session_abc123...",
  "url": null,
  "user": { ... }
}

Errors

StatusCondition
400Missing or invalid email format
401Invalid credentials

Sign In with Username

POST /sign-in/username
Content-Type: application/json

Request

{
  "username": "alice",
  "password": "secure_password"
}

Response

Same format as email sign-in, including redirect and url.

Errors

StatusCondition
400Missing username or password
401Invalid credentials

On this page