Prerequisites
Before starting, make sure you’ve completed the Setup guide and have theMoonKeyProvider configured in your application.
Step 1: Authenticate users with email
MoonKey makes authentication simple with email-based one-time passwords (OTPs). While this guide focuses on email authentication, MoonKey supports multiple methods including OAuth providers, wallet signatures, and WebAuthn.Using the email authentication hook
TheuseLoginWithEmail hook provides everything you need to implement email OTP authentication:
Make sure your component is wrapped by
MoonKeyProvider to use MoonKey hooks. See the Setup guide for details.Building a complete login flow
Here’s a complete example showing how to collect an email, send an OTP, and authenticate the user:Step 2: Create embedded wallets
MoonKey can automatically create self-custodial wallets for your users during the authentication flow. Configure this in yourMoonKeyProvider:
'always'— Automatically create a wallet for every user upon login'none'— Disable automatic wallet creation (create manually when needed)
MoonKey supports embedded wallet creation on both Ethereum (and all EVM chains) and Solana. Choose your blockchain when importing the provider.
Manual wallet creation
Alternatively, you can create wallets on-demand when users need them:Step 3: Send transactions
Once users have embedded wallets, your app can prompt them to sign and send blockchain transactions.- Ethereum
- Solana
Use the
useSendTransaction hook to send transactions on Ethereum and EVM-compatible chains:Accessing user data
After authentication, access user information with theuseMoonKey hook:
Handling logout
Allow users to log out and clear their session:Troubleshooting
”User is not authenticated” error
Make sure you’re checking theisAuthenticated state before attempting wallet operations:
Transactions failing
Common reasons transactions fail:- Insufficient balance in the wallet
- Invalid recipient address
- Network congestion or RPC issues
- User rejected the transaction
Hook errors
If you see “Hook can only be used inside MoonKeyProvider”, ensure:- Your component is wrapped by
MoonKeyProvider - You’re importing from the correct package (
@moon-key/react-auth/ethereumor@moon-key/react-auth/solana) - The provider is mounted before components using hooks