Docs
Email Handling Flow
Email Handling Flow
Email delivery and communication using Resend service
Overview
Manages email delivery for authentication and user communication using Resend service. Primarily handles magic link authentication emails with custom React Email templates.
Main libraries/services:
resend- Email delivery service@react-email/components- Email template componentsnext-auth/providers/resend- NextAuth email provider integration
File Map
š lib/email.ts - Email sending logic and Resend configuration
š emails/magic-link-email.tsx - React Email template for magic links
š auth.config.ts - Resend provider configuration
š components/forms/user-auth-form.tsx - Email authentication UI
š config/site.ts - Site configuration for email templates
Step-by-Step Flow
Magic Link Email Authentication
- User enters email address in login form
UserAuthFormcallssignIn("resend", { email })- NextAuth generates secure magic link token
sendVerificationRequestfunction is triggered- User lookup is performed via
getUserByEmail - Email subject is determined based on user verification status:
- New users: "Activate your account"
- Existing users: "Sign-in link for [Site Name]"
- React Email template is rendered with user data
- Email is sent via Resend API with custom headers
- User receives email with magic link button
- User clicks link to authenticate and access dashboard
Email Template Rendering
MagicLinkEmailcomponent receives props:firstName: User's nameactionUrl: Generated magic linkmailType: "login" or "register"siteName: Application name
- Template renders with Tailwind CSS styling
- Conditional content based on email type
- Security messaging for link expiration (24 hours)
Development vs Production
- Development: Emails sent to
delivered@resend.devfor testing - Production: Emails sent to actual user email addresses
- Custom headers prevent Gmail threading issues
Data Flow Diagram
[Login Form] ā [NextAuth] ā [Magic Link Generation] ā [Email Template] ā [Resend API] ā [User Inbox]
ā
[User Clicks Link] ā [Token Validation] ā [Session Creation] ā [Dashboard Access]
Email Templates
- Magic Link Email: Authentication and account activation
- Newsletter Form: Marketing email collection (referenced but not implemented)
Configuration
- Resend API Key: Required for email delivery
- From Address: Configured in environment variables
- Email Headers: Custom headers to prevent threading issues
- Template Styling: Tailwind CSS for responsive email design
Notes and TODOs
- ā Magic link authentication working with Resend
- ā React Email templates with Tailwind styling
- ā Development/production email routing
- ā Custom email headers for better deliverability
- ā ļø Email verification request is commented out in auth.config.ts
- š Add email templates for password reset
- š Implement newsletter subscription emails
- š Add email templates for subscription notifications
- š Consider adding email templates for admin notifications
- š Add email delivery tracking and analytics