Send Email Hook Setup Guide
Overview
This Send Email Hook replaces Supabase’s default email templates with SendGrid’s dynamic templates, providing platform-specific email handling for mobile and web applications.Features
- Platform detection (mobile vs web)
- SendGrid dynamic templates
- Conditional email content (code-only for mobile, link-only for web)
- Support for all 6 Supabase auth email types: signup, recovery, magic link, email change, invite, and reauthenticate emails
Setup Steps
1. Environment Variables
Add these environment variables to your Supabase project:2. SendGrid Template IDs
The current template IDs configured in the system:email_change and reauthenticate templates still need to be configured with actual SendGrid template IDs.
3. SendGrid Dynamic Template Setup
Create dynamic templates in SendGrid with these variables:Available Variables for Templates
Common Variables (Available in All Templates):- Signup Template (
d-f616cf918fea42b2ac48870b0953154b){{subject}}→ “Confirm Your Signup - ADHD Simple”
- Recovery Template (
d-4264768063af464bb138244a4cfc011e){{subject}}→ “Reset Your Password - ADHD Simple”
- Magic Link Template (
d-615be9bd4d34493f80a7c7ba283516a3){{subject}}→ “Magic Link Login - ADHD Simple”
- Invite Template (
d-6f15a805896e468fb8ad74cb69d62c5f){{subject}}→ “You’ve Been Invited - ADHD Simple”
- Email Change Template (TODO)
{{subject}}→ “Confirm Email Change - ADHD Simple”{{new_email}}→ New email address (if applicable)
- Reauthenticate Template (TODO)
{{subject}}→ “Security Verification Required - ADHD Simple”
{{platform}}will be “mobile” or “web”- Use
{{#equals platform "mobile"}}for conditional content in SendGrid templates - Use
{{#if first_name}}for checking if variables exist
Template Examples
Signup Template:4. Deploy the Function
5. Configure Auth Hook
- Go to Supabase Dashboard > Auth > Hooks
- Create a new “Send Email” hook
- Set the URL to:
https://your-project-id.supabase.co/functions/v1/send-email - Generate and copy the hook secret to your environment variables (format:
v1,whsec_base64_secret)
6. Platform Detection Logic
The hook determines platform based on (in priority order):- User metadata:
user.user_metadata.platform === "mobile" - App metadata:
user.app_metadata.platform === "mobile" - Redirect URL patterns:
- Mobile indicators:
adhdsimple://,exp://,/mobile/,app.adhdsimple.co.uk - Default:
web
- Mobile indicators:
7. URL Construction
The confirmation URLs are built with the correct Supabase auth endpoint:apikey parameter is needed - Supabase handles this internally.
8. Testing
Test different email types:Platform-Specific Behavior
Mobile Apps
- Shows only the 6-digit verification code
- Code expires in 1 hour
- No clickable links (users enter code manually)
- Platform detected via user metadata or redirect URL patterns
Web Apps
- Shows only confirmation button/link
- No verification code displayed
- Standard email confirmation flow with clickable links
- Users are redirected after clicking the link
Troubleshooting
Common Issues
-
“No API key found in request” error:
- Fixed by ensuring URL uses
/auth/v1/verifyendpoint (not/auth/v1) - No
apikeyparameter needed in the URL
- Fixed by ensuring URL uses
-
Hook secret format: Must start with
v1,whsec_ -
Template IDs: Must start with
d-and be valid SendGrid dynamic template IDs - Platform detection: Check user metadata and redirect URL patterns
-
SendGrid templates: Ensure Handlebars syntax is correct (use
{{#equals platform "mobile"}}for comparisons,{{#if first_name}}for existence checks)
Logs
Check function logs:Testing Platform Detection
Set platform explicitly in user metadata:Configuration Status
✅ Configured and Working:- Signup emails
- Recovery emails
- Magic link emails
- Invite emails
- Email change emails (template ID needed)
- Reauthenticate emails (template ID needed)
Next Steps
- Create SendGrid templates for
email_changeandreauthenticateactions - Update the template IDs in
packages/supabase/functions/send-email/index.ts - Test all email flows in both mobile and web contexts
- Monitor function logs for any issues