Skip to main content

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.
Enable email authentication in the MoonKey Dashboard under Login Methods before implementing this feature.
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 the useMoonKey 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 an EmailOtpConfig object to the MoonKeyProvider:
boolean
Skip the success screen after successful verification
number
OTP expiration time in seconds (default: 600 = 10 minutes)
number
How long to display the success message in milliseconds
string
Custom title for the email verification screen
string
Custom text for the resend code button
number
Time in seconds before resend is allowed (default: 60)
string
Custom message shown to new users after verification
string
Custom message shown to existing users after verification
string
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
Array of login methods to show. Options: 'email', 'google', 'apple', 'wallet'. If not provided, uses login methods configured in the provider.
object
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
boolean
If true, prevents new user signups and only allows existing users to login.
string
Specify which wallet type(s) to support. Options: 'ethereum', 'solana', or 'ethereum-or-solana'.

Returns

void
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

Response

Backend Implementation Example

Configuration

Enable email authentication

  1. Log in to the MoonKey Dashboard
  2. Navigate to Login Methods
  3. Enable Email OTP
  4. Save your changes

Customize email templates

You can customize the OTP email template in the dashboard:
  1. Navigate to Email Templates
  2. Select OTP Email
  3. Customize the subject, content, and styling
  4. 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
You can customize this behavior using the 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
You can customize error messages using the 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

  1. Check spam/junk folders
  2. Verify email domain is not blocked
  3. Check email template configuration in dashboard
  4. Verify DKIM and SPF records (for custom domains)

Code verification fails

  1. Ensure code hasn’t expired (10-minute window)
  2. Check for typos in the code
  3. Verify the email address matches
  4. Check that the code hasn’t been used already

Session not created

  1. Verify session_expires_in is within valid range (5 minutes to 366 days)
  2. Check API key has correct permissions
  3. Ensure backend is properly handling the response