Skip to main content

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:
Note: The 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):
Template-Specific Variables:
  • 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 Detection:
  • {{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:
Recovery Template:
Magic Link Template:
Invite Template:

4. Deploy the Function

5. Configure Auth Hook

  1. Go to Supabase Dashboard > Auth > Hooks
  2. Create a new “Send Email” hook
  3. Set the URL to: https://your-project-id.supabase.co/functions/v1/send-email
  4. 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):
  1. User metadata: user.user_metadata.platform === "mobile"
  2. App metadata: user.app_metadata.platform === "mobile"
  3. Redirect URL patterns:
    • Mobile indicators: adhdsimple://, exp://, /mobile/, app.adhdsimple.co.uk
    • Default: web

7. URL Construction

The confirmation URLs are built with the correct Supabase auth endpoint:
Important: No 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

  1. “No API key found in request” error:
    • Fixed by ensuring URL uses /auth/v1/verify endpoint (not /auth/v1)
    • No apikey parameter needed in the URL
  2. Hook secret format: Must start with v1,whsec_
  3. Template IDs: Must start with d- and be valid SendGrid dynamic template IDs
  4. Platform detection: Check user metadata and redirect URL patterns
  5. 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
⚠️ Needs Configuration:
  • Email change emails (template ID needed)
  • Reauthenticate emails (template ID needed)

Next Steps

  1. Create SendGrid templates for email_change and reauthenticate actions
  2. Update the template IDs in packages/supabase/functions/send-email/index.ts
  3. Test all email flows in both mobile and web contexts
  4. Monitor function logs for any issues