OAuth Authentication
MoonKey enables users to sign in using familiar OAuth providers like Google and Apple. OAuth authentication provides a seamless login experience that users are already comfortable with, integrated into your application in just a few lines of code.Supported OAuth Providers
MoonKey currently supports the following OAuth providers:- Google - Sign in with Google accounts
- Apple - Sign in with Apple ID
Additional OAuth providers (Microsoft, Discord, GitHub, etc.) are coming soon. Contact us if you need support for a specific provider.
Configuration
Dashboard Setup
Before implementing OAuth authentication, you must configure your OAuth credentials for each provider:- Log in to the MoonKey Dashboard
- Navigate to Login Methods → OAuth
- Select the provider you want to enable (Google or Apple)
- Enter your OAuth Client ID and Client Secret
- Configure your redirect URLs
- Save your changes
Getting OAuth Credentials
Google OAuth
- Go to the Google Cloud Console
- Create a new project or select an existing one
- Enable the Google+ API
- Go to Credentials → Create Credentials → OAuth 2.0 Client ID
- Configure the consent screen
- Add authorized redirect URIs provided by MoonKey
- Copy the Client ID and Client Secret to the MoonKey Dashboard
Apple OAuth
- Go to Apple Developer
- Navigate to Certificates, Identifiers & Profiles
- Create a new Services ID
- Enable Sign in with Apple
- Configure the redirect URLs
- Generate a private key
- Copy the credentials to the MoonKey Dashboard
Using the React SDK
MoonKey provides two ways to implement OAuth authentication:useMoonKeyhook withstart()- Unified authentication flow with all login methodsuseLoginWithOAuthhook - Dedicated OAuth hook for more control
Import
Method 1: Using useMoonKey
TheuseMoonKey hook provides a unified way to trigger authentication with multiple methods.
Basic Usage
Open the authentication modal with OAuth providers:Google-only Login
Show only Google authentication:Apple-only Login
Show only Apple authentication:Multiple OAuth Providers
Show multiple OAuth options:Combined with Other Methods
Combine OAuth with email and wallet authentication:Login-only (Disable Signup)
Prevent new user signups via OAuth:Method 2: Using useLoginWithOAuth
TheuseLoginWithOAuth hook provides dedicated OAuth functionality with more control over the authentication flow.
Import and Usage
loginWithOAuth Method
Initiates the OAuth authentication flow for a specific provider:Parameters
string
required
The OAuth provider to use. Options:
'google' or 'apple'.boolean
If true, prevents new user signups and only allows existing users to login.
Tracking Flow State
Track the OAuth authentication state:'initial' | 'loading' | 'done' | 'error'
The current state of the OAuth flow.
Error | null
The error that occurred during the OAuth flow (only present when status is ‘error’).
Complete Example
With Callbacks
You can pass callbacks to handle success and error cases:Login-only Mode
Prevent new signups and only allow existing users:Provider Configuration
You can configure which OAuth providers are available by default in theMoonKeyProvider:
Using the REST API
For backend implementations or custom integrations, use the MoonKey REST API.Initiate OAuth Flow
Start the OAuth authentication flow:Verify OAuth Token
After the user completes OAuth authentication and is redirected back to your application, verify the OAuth code:Response
Backend Implementation Example
Security Best Practices
Redirect URL Validation
Configure allowed OAuth redirect URLs in the MoonKey Dashboard to restrict where users can be redirected after authentication:- Navigate to Redirect URLs in the dashboard
- Add your application’s callback URLs
- Only whitelisted URLs will be accepted
State Parameter
Thestate parameter helps prevent CSRF attacks:
- MoonKey generates a unique state value for each OAuth flow
- Store this value in your session
- Verify it matches when handling the callback
- Reject requests with mismatched state values
HTTPS Only
- Always use HTTPS in production
- Configure OAuth providers to only accept HTTPS redirect URLs
- Never use HTTP for OAuth flows
Token Storage
- Store session tokens securely (HttpOnly cookies on web)
- Never expose session tokens in URLs or logs
- Use secure, httpOnly cookies when possible
User Experience
Loading States
Show appropriate loading states during OAuth flow:Error Handling
The MoonKey SDK automatically handles common OAuth errors:- User cancellation - When users close the OAuth popup or decline
- Invalid credentials - When OAuth configuration is incorrect
- Network errors - Connection issues during the flow
- State mismatch - CSRF protection validation failures
Popup vs Redirect
MoonKey uses popup windows for OAuth flows by default, which:- Keep users on your page
- Provide a seamless experience
- Work across all modern browsers
Testing
Test Mode
During development, use test API keys to:- Test OAuth flows without affecting production users
- Validate your integration
- Debug callback handling
Test with Real Providers
Always test with actual OAuth providers:- Google test accounts
- Apple test accounts
- Verify the complete flow works end-to-end
Common Test Scenarios
- First-time user signup via OAuth
- Existing user login via OAuth
- User cancels OAuth flow
- Network errors during OAuth
- Invalid or expired OAuth codes
- Multiple OAuth accounts linked to same user
Troubleshooting
OAuth flow doesn’t start
Possible causes:- OAuth provider not enabled in dashboard
- Invalid OAuth credentials configured
- Missing redirect URLs in dashboard
- Verify provider is enabled in Login Methods
- Check OAuth Client ID and Secret are correct
- Add your callback URLs to Redirect URLs
Redirect URL mismatch error
Cause: The redirect URL doesn’t match what’s configured in your OAuth provider. Solution:- Check the redirect URL in MoonKey Dashboard
- Ensure it matches exactly in your OAuth provider settings (Google Cloud Console, Apple Developer)
- Include the protocol (
https://) and any path components
State parameter mismatch
Cause: The state value doesn’t match between initial request and callback. Solutions:- Ensure you’re storing the state in your session correctly
- Check for session timeout issues
- Verify cookies are enabled
User information not available
Cause: OAuth scopes might not include profile information. Solution: Check your OAuth provider configuration includes the necessary scopes:- Google:
email,profile,openid - Apple:
email,name
Provider-Specific Notes
Google OAuth
- Supports email and profile information
- Works on web and most browsers
- May not work in some in-app browsers (Facebook, Instagram apps)
- Requires verified domain for production use
Apple OAuth
- Requires Apple Developer account
- Email may be hidden/private on first login
- Name is only provided on first authentication
- Works across web, iOS, and macOS