/api/auth/*.
Basic Structure
auth Settings
Type
Type:BetterAuthOptions (configuration type from better-auth library)
Email/Password Authentication
Social Login (Google)
Social Login (GitHub)
Customizing basePath
The default path is/api/auth. To change it:
Entity Generation
You need to generate the required entities before using better-auth.Generate Entities via CLI
If a User entity already exists, only missing fields will be added when running the command.
Generate Entities with Plugins
To use better-auth plugins, specify the required plugins with the--plugins option:
Field Mapping
Since Sonamu uses snake_case column names, better-auth’s camelCase field names are automatically mapped:Authentication APIs
Once better-auth is registered, the following APIs become automatically available:Sign Up
Sign In
Sign Out
Current Session
Social Login (Google)
Accessing User Information from Context
In authenticated requests, you can access user information through Context.User Type
Session Type
Access Control with Guards
Client-Side Integration
Using with React
Plugin Configuration
To use better-auth plugins, add them to theauth.plugins array in sonamu.config.ts.
Two-Factor Authentication (2FA)
Enable TOTP-based two-factor authentication:POST /api/auth/two-factor/enable- Start 2FA setupPOST /api/auth/two-factor/verify- Verify 2FA codePOST /api/auth/two-factor/disable- Disable 2FA
Admin Plugin
Supports user roles, banning, and impersonation:role- User role (default: “user”)banned- Ban statusban_reason- Ban reasonban_expires- Ban expiration time (Unix timestamp)
Username Plugin
Allows login with username instead of email:username- Normalized username (lowercase, unique index)display_username- Display username (preserves original case)
Phone Number Plugin
Supports phone number verification:phone_number- Phone number (unique index)phone_number_verified- Phone number verification status
Passkey Plugin
Supports WebAuthn/FIDO2 based passkey authentication:passkeys- User passkey information (public key, credential ID, etc.)
POST /api/auth/passkey/generate-register-options- Generate passkey registration optionsPOST /api/auth/passkey/verify-registration- Verify passkey registrationPOST /api/auth/passkey/generate-authentication-options- Generate passkey authentication optionsPOST /api/auth/passkey/verify-authentication- Verify passkey authentication
The
@better-auth/passkey package is required to use the Passkey plugin.SSO Plugin
Supports SSO login through external IdPs (OIDC, SAML):sso_providers- SSO provider settings (including OIDC/SAML configuration)
The
@better-auth/sso package is required to use the SSO plugin.API Key Plugin
Supports API key based authentication:api_keys- API key information (hashed key, rate limit settings, etc.)
POST /api/auth/api-key/create- Create API keyPOST /api/auth/api-key/revoke- Revoke API keyGET /api/auth/api-key/list- List API keys
JWT Plugin
Supports JWT token issuance and JWKS key management:jwks- JSON Web Key Set information (public key, private key)
GET /api/auth/.well-known/jwks.json- JWKS endpointPOST /api/auth/jwt/generate- Generate JWT token
Organization Plugin
Supports organization, member, invitation, and team management:organizations- Organization informationmembers- Organization membersinvitations- Organization invitationsteams- Teamsteam_members- Team members
active_organization_id- Current active organization IDactive_team_id- Current active team ID
POST /api/auth/organization/create- Create organizationPOST /api/auth/organization/invite- Invite memberPOST /api/auth/organization/accept-invitation- Accept invitationPOST /api/auth/organization/set-active- Set active organization
Anonymous Plugin
Supports anonymous user authentication. Allows creating temporary users without sign-up:is_anonymous- Whether the user is anonymous
POST /api/auth/sign-in/anonymous- Anonymous loginPOST /api/auth/anonymous/link- Link anonymous account to a regular account
Using Multiple Plugins Together
Each plugin’s schema (
*_SCHEMA) maps Sonamu’s snake_case column names to better-auth’s camelCase
field names. It must be passed along with the corresponding plugin.Practical Examples
Basic Configuration
Social Login + Email Verification
Important Notes
1. Entity Generation Required
2. Environment Variables Setup
3. CORS Configuration
When the client runs on a different domain:4. Compatibility with Existing User Entity
If a User entity already exists, runningpnpm sonamu auth generate will only add missing fields. Existing data is preserved.