Phone + OTP (Primary for Saudi Arabia)
Phone-based authentication with one-time passwords (OTP) is the primary authentication method for Saudi Arabian users. This flow provides a passwordless experience while maintaining security.Request OTP
User submits their phone number to receive an OTP code.Request:Response:The phone number is automatically normalized to E.164 format and validated. The OTP is generated (6 digits by default) and sent via your configured SMS provider.
The OTP flow automatically creates a new user account if the phone number doesn’t exist. The
phoneVerified field is set to true upon successful verification.OTP Configuration
You can customize OTP behavior in your auth configuration:packages/core/src/routes/otp-send.ts:39-45 and packages/core/src/routes/otp-verify.ts:44-50 for implementation details.
Email + Password
Traditional credential-based authentication with email and password.- Sign Up
- Sign In
Create a new account with email and password.Request:Response:Passwords are hashed using bcrypt before storage. The minimum password length is 8 characters.See
packages/core/src/routes/sign-up.ts:52-60 for password hashing implementation.OAuth Providers
Arraf Auth supports OAuth 2.0 authentication with external providers like Google, GitHub, and custom providers.Initiate OAuth Flow
Redirect user to the OAuth provider’s authorization page.Request:This endpoint:
- Generates a random state parameter for CSRF protection
- Generates PKCE code verifier and challenge (for enhanced security)
- Stores state and verifier in secure HTTP-only cookies
- Redirects user to provider’s authorization URL
packages/core/src/routes/oauth-start.ts:18-36 for state and PKCE generation.Provider Authorization
User authorizes your application on the provider’s page and is redirected back to your callback URL.
Handle Callback
The callback route processes the authorization code and creates a session.Callback URL:The callback handler:
- Validates the state parameter to prevent CSRF attacks (
packages/core/src/routes/oauth-callback.ts:40-46) - Exchanges the authorization code for access tokens (
packages/core/src/routes/oauth-callback.ts:48-53) - Fetches the user’s profile from the provider (
packages/core/src/routes/oauth-callback.ts:55-60) - Creates or finds existing user by email (
packages/core/src/routes/oauth-callback.ts:66-85) - Links the OAuth account to the user (
packages/core/src/routes/oauth-callback.ts:87-103) - Creates a session and sets the cookie (
packages/core/src/routes/oauth-callback.ts:109-116) - Redirects to the application
Configuring OAuth Providers
OAuth providers must implement the
OAuthProvider interface defined in packages/core/src/types.ts:118-127, which includes methods for getting authorization URLs, exchanging codes, and fetching user profiles.Flow Comparison
| Feature | Phone + OTP | Email + Password | OAuth |
|---|---|---|---|
| Passwordless | Yes | No | Yes |
| Saudi Arabia Optimized | Yes | No | No |
| Auto Sign-up | Yes | No | Yes |
| Verification Required | Yes (phone) | Optional (email) | Provider-dependent |
| Security | High (time-limited OTP) | High (bcrypt hashed) | High (provider-managed) |
| User Experience | Fast | Traditional | Convenient |
Plugin Hooks
All authentication flows support plugin hooks for custom logic:Next Steps
Session Management
Learn how sessions are created, stored, and validated
Plugin System
Extend authentication flows with custom logic