Email Authentication
MoonKey enables users to login to your application with email-based one-time passcodes (OTP). With email authentication, your application can verify ownership of a user’s email address without requiring passwords.For the complete and latest API reference, including all configuration options, check the @moon-key/react-auth npm package.
Using the React SDK
To authenticate your users, use theuseMoonKey hook and call the start() method to initiate the authentication flow.
Import
Configuration
Email OTP Configuration
You can customize the email OTP flow by passing anEmailOtpConfig object to the MoonKeyProvider:
Skip the success screen after successful verification
OTP expiration time in seconds (default: 600 = 10 minutes)
How long to display the success message in milliseconds
Custom title for the email verification screen
Custom text for the resend code button
Time in seconds before resend is allowed (default: 60)
Custom message shown to new users after verification
Custom message shown to existing users after verification
Custom success message after verification
Example Configuration
Using the start() method
Start authentication flow
Initiate the authentication flow with email or other login methods:Parameters
Array of login methods to show. Options:
'email', 'google', 'apple', 'wallet'. If not provided, uses login methods configured in the provider.Pre-fill the authentication form with an email or phone number.
type: Either'email'or'phone'value: The email address or phone number to prefill
If true, prevents new user signups and only allows existing users to login.
Specify which wallet type(s) to support. Options:
'ethereum', 'solana', or 'ethereum-or-solana'.Returns
Opens the authentication modal/flow. No return value.
Basic Usage
Email-only login
To show only email authentication:Customizing Messages and Behavior
Here’s an example with custom configuration for a better user experience:Prefill email address
Prefill the email input when you know the user’s email:Login-only (no signup)
Prevent new user signups and only allow existing users to login:Multiple login methods
Show multiple authentication options:Using the REST API
For custom implementations or backend integration, use the MoonKey REST API directly.Send Email OTP
Send a one-time passcode to the user’s email:Response
Verify Email OTP
Verify the OTP code and authenticate the user:Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
email | string | Yes | The user’s email address |
code | string | Yes | The 6-digit OTP code |
session_expires_in | number | No | Session duration in minutes (default: 10080 = 7 days) |
Response
Backend Implementation Example
Configuration
Enable email authentication
- Log in to the MoonKey Dashboard
- Navigate to Login Methods
- Enable Email OTP
- Save your changes
Customize email templates
You can customize the OTP email template in the dashboard:- Navigate to Email Templates
- Select OTP Email
- Customize the subject, content, and styling
- Preview and save
Best Practices
Security
- Rate limiting: Implement rate limits on OTP sending to prevent abuse
- Code expiration: OTP codes expire after 10 minutes
- Maximum attempts: Limit verification attempts to prevent brute force
- Secure transmission: Always use HTTPS for API calls
User Experience
- Clear instructions: Tell users where to find the code
- Resend option: Allow users to request a new code if needed
- Loading states: Show loading indicators during API calls
- Error handling: Provide clear error messages
Implementation
The MoonKey SDK handles the entire email OTP flow automatically, including:- Sending the OTP code
- Displaying the code input form
- Verifying the code
- Managing resend functionality with countdown timer
- Error handling and validation
emailOtp configuration in the provider (see Configuration section above).
Error Handling
The MoonKey SDK automatically handles common errors in the authentication flow:- Invalid or expired codes - Shows an error message and allows the user to request a new code
- Rate limiting - Prevents too many code requests in a short time period
- Invalid email format - Validates email addresses before sending codes
- Network errors - Displays appropriate error messages for connectivity issues
emailOtp configuration options like verifiedNewUserMessage, verifiedExistingUserMessage, and verifiedSuccessMessage.
Testing
Test with different email providers
Test your email authentication flow with various email providers:- Gmail
- Outlook/Hotmail
- Yahoo
- Corporate email domains
- Custom domains
Test edge cases
- Invalid email format
- Non-existent email addresses
- Expired codes
- Already used codes
- Multiple login attempts
Troubleshooting
Emails not being received
- Check spam/junk folders
- Verify email domain is not blocked
- Check email template configuration in dashboard
- Verify DKIM and SPF records (for custom domains)
Code verification fails
- Ensure code hasn’t expired (10-minute window)
- Check for typos in the code
- Verify the email address matches
- Check that the code hasn’t been used already
Session not created
- Verify
session_expires_inis within valid range (5 minutes to 366 days) - Check API key has correct permissions
- Ensure backend is properly handling the response