Email Verification using Magic Link
BuilderKit provides a secure and user-friendly way to implement email verification using magic links. This method eliminates the need for passwords and enhances the user experience by simplifying the verification process.
Key features:
- Displays a form for users to enter their email address
- Handles form submission and triggers the magic link sending process
- Shows error messages or success messages based on the operation result
Prerequisites
Before you start, ensure you have the following:
- A BuilderKit project set up
- A Resend account for sending emails
- Environment variables properly configured
Configuration
-
Generate a secure JWT_SECRET_KEY (this is required to create a secure hash that is used to verify the magic link url):
To create a strong JWT_SECRET_KEY, you can use a cryptographically secure random string generator. Here are two methods:
a. Using OpenSSL (recommended for production):
openssl rand -base64 32
b. Using Node.js (good for development):
node -e "console.log(require('crypto').randomBytes(32).toString('base64'));"
The output will be a random string. Use this as your JWT_SECRET_KEY.
-
Set up the necessary environment variables in your
.env
file:JWT_SECRET_KEY=your_generated_jwt_secret_key RESEND_API_KEY=your_resend_api_key
Make sure to replace the placeholder values with your actual secret key, app URL, and Resend API key.
-
Setup email integration with resend. Follow the documentation (opens in a new tab) for step by step process.
Implementation
- The email verification page is implemented and live in
/email-verification
page. This page allows users to enter their email address and request a magic link. - The codebase uses Resend (opens in a new tab) to send emails to the entered email id.
- Once the user clicks on the magic link in the email it redirects the user to
/email-verification/verify
page. This page verifies the magic link and create a session for the user.
You can update any section or feature if required visiting the same file.
Security Considerations
-
JWT Secret Key: Ensure that your JWT secret key is kept secure and not exposed in your codebase or client-side scripts. Regularly rotate this key in production environments.
-
Token Expiration: The JWT tokens are set to expire after 1 hour. Adjust this duration based on your security requirements.
-
HTTPS: Always use HTTPS in production to encrypt the magic links in transit.
-
Rate Limiting: Consider implementing rate limiting on the magic link generation to prevent abuse.
Customization
You can customize various aspects of the email verification process:
-
Email Template: Modify the email content in the
sendMagicLink
function to match your branding and messaging. -
Token Expiration: Adjust the
expiresIn
option in thegenerateJWT
function to change the token expiration time. -
Error Messages: Customize error messages in the
EmailVerification
andVerify
components to provide more specific feedback to users.
Troubleshooting
-
If emails are not being sent, check your Resend API key and sender email configuration.
-
Ensure that your app's URL is correctly set in the
APP_URL
environment variable for proper magic link generation. -
If verification fails, check that the
JWT_SECRET_KEY
is consistent across your development and production environments.