Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Add Auth Hook Resend Example. (#28805)

Thor 雷神 Schaeff2ae41b620e68
diff --git a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
index d5c8479e33f..94fb0203d4e 100644
--- a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
+++ b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
@@ -1223,6 +1223,10 @@ export const functions: NavMenuConstant = {
name: 'Examples',
url: undefined,
items: [
+ {
+ name: 'Auth Send Email Hook',
+ url: '/guides/functions/examples/auth-send-email-hook-react-email-resend',
+ },
{
name: 'CORS support for invoking from the browser',
url: '/guides/functions/cors',
@@ -1295,6 +1299,10 @@ export const functions: NavMenuConstant = {
url: '/guides/functions/examples/sentry-monitoring',
},
{ name: 'OpenAI API', url: '/guides/ai/examples/openai' },
+ {
+ name: 'React Email',
+ url: '/guides/functions/examples/auth-send-email-hook-react-email-resend',
+ },
{
name: 'Sending Emails with Resend',
url: '/guides/functions/examples/send-emails',
diff --git a/apps/docs/content/guides/auth/auth-hooks/send-email-hook.mdx b/apps/docs/content/guides/auth/auth-hooks/send-email-hook.mdx
index be8548bfab9..d8945b23bb2 100644
--- a/apps/docs/content/guides/auth/auth-hooks/send-email-hook.mdx
+++ b/apps/docs/content/guides/auth/auth-hooks/send-email-hook.mdx
@@ -453,7 +453,9 @@ select
defaultActiveId="http-send-email-with-resend"
>
<TabPanel id="http-send-email-with-resend" label="Use Resend as an email provider">
-You can configure [Resend](https://resend.com/) as the custom email provider through the "Send Email" hook. This allows you to take advantage of Resend's developer-friendly APIs to send emails and leverage [React Email](https://react.email/) for managing your email templates. If you want to send emails through the Supabase Resend integration, which uses Resend's SMTP server, check out [this integration](/partners/integrations/resend) instead.
+You can configure [Resend](https://resend.com/) as the custom email provider through the "Send Email" hook. This allows you to take advantage of Resend's developer-friendly APIs to send emails and leverage [React Email](https://react.email/) for managing your email templates. For a more advanced React Email tutorial, refer to [this guide](/docs/guides/functions/examples/auth-send-email-hook-react-email-resend).
+
+If you want to send emails through the Supabase Resend integration, which uses Resend's SMTP server, check out [this integration](/partners/integrations/resend) instead.
Create a `.env` file with the following environment variables:
@@ -462,6 +464,12 @@ RESEND_API_KEY=your_resend_api_key
SEND_EMAIL_HOOK_SECRET=<base64_secret>
```
+<Admonition type="info">
+
+You can generate the secret in the [Auth Hooks](/dashboard/project/_/auth/hooks) section of the Supabase dashboard. Make sure to remove the `v1,whsec_` prefix!
+
+</Admonition>
+
Set the secrets in your Supabase project:
```bash
diff --git a/apps/docs/content/guides/functions/examples/auth-send-email-hook-react-email-resend.mdx b/apps/docs/content/guides/functions/examples/auth-send-email-hook-react-email-resend.mdx
new file mode 100644
index 00000000000..3eeaded1bed
--- /dev/null
+++ b/apps/docs/content/guides/functions/examples/auth-send-email-hook-react-email-resend.mdx
@@ -0,0 +1,302 @@
+---
+title: 'Custom Auth Emails with React Email and Resend'
+description: 'Use the send email hook to send custom auth emails with React Email and Resend in Supabase Edge Functions.'
+tocVideo: 'tlA7BomSCgU'
+---
+
+Use the [send email hook](/docs/guides/auth/auth-hooks/send-email-hook?queryGroups=language&language=http) to send custom auth emails with [React Email](https://react.email/) and [Resend](https://resend.com/) in Supabase Edge Functions.
+
+<Admonition type="info">
+
+Prefer to jump straight to the code? [Check out the example on GitHub](https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/auth-hook-react-email-resend).
+
+</Admonition>
+
+### Prerequisites
+
+To get the most out of this guide, you’ll need to:
+
+- [Create a Resend API key](https://resend.com/api-keys)
+- [Verify your domain](https://resend.com/domains)
+
+Make sure you have the latest version of the [Supabase CLI](https://supabase.com/docs/guides/cli#installation) installed.
+
+### 1. Create Supabase function
+
+Create a new function locally:
+
+```bash
+supabase functions new send-email
+```
+
+### 2. Edit the handler function
+
+Paste the following code into the `index.ts` file:
+
+```tsx supabase/functions/send-email/index.ts
+import React from 'npm:[email protected]'
+import { Webhook } from 'https://esm.sh/[email protected]'
+import { Resend } from 'npm:[email protected]'
+import { renderAsync } from 'npm:@react-email/[email protected]'
+import { MagicLinkEmail } from './_templates/magic-link.tsx'
+
+const resend = new Resend(Deno.env.get('RESEND_API_KEY') as string)
+const hookSecret = Deno.env.get('SEND_EMAIL_HOOK_SECRET') as string
+
+Deno.serve(async (req) => {
+ if (req.method !== 'POST') {
+ return new Response('not allowed', { status: 400 })
+ }
+
+ const payload = await req.text()
+ const headers = Object.fromEntries(req.headers)
+ const wh = new Webhook(hookSecret)
+ try {
+ const {
+ user,
+ email_data: { token, token_hash, redirect_to, email_action_type },
+ } = wh.verify(payload, headers) as {
+ user: {
+ email: string
+ }
+ email_data: {
+ token: string
+ token_hash: string
+ redirect_to: string
+ email_action_type: string
+ site_url: string
+ token_new: string
+ token_hash_new: string
+ }
+ }
+
+ const html = await renderAsync(
+ React.createElement(MagicLinkEmail, {
+ supabase_url: Deno.env.get('SUPABASE_URL') ?? '',
+ token,
+ token_hash,
+ redirect_to,
+ email_action_type,
+ })
+ )
+
+ const { error } = await resend.emails.send({
+ from: 'welcome <[email protected]>',
+ to: [user.email],
+ subject: 'Supa Custom MagicLink!',
+ html,
+ })
+ if (error) {
+ throw error
+ }
+ } catch (error) {
+ console.log(error)
+ return new Response(
+ JSON.stringify({
+ error: {
+ http_code: error.code,
+ message: error.message,
+ },
+ }),
+ {
+ status: 401,
+ headers: { 'Content-Type': 'application/json' },
+ }
+ )
+ }
+
+ const responseHeaders = new Headers()
+ responseHeaders.set('Content-Type', 'application/json')
+ return new Response(JSON.stringify({}), {
+ status: 200,
+ headers: responseHeaders,
+ })
+})
+```
+
+### 3. Create React Email Templates
+
+Create a new folder `_templates` and create a new file `magic-link.tsx` with the following code:
+
+```tsx supabase/functions/send-email/_templates/magic-link.tsx
+import {
+ Body,
+ Container,
+ Head,
+ Heading,
+ Html,
+ Link,
+ Preview,
+ Text,
+} from 'npm:@react-email/[email protected]'
+import * as React from 'npm:[email protected]'
+
+interface MagicLinkEmailProps {
+ supabase_url: string
+ email_action_type: string
+ redirect_to: string
+ token_hash: string
+ token: string
+}
+
+export const MagicLinkEmail = ({
+ token,
+ supabase_url,
+ email_action_type,
+ redirect_to,
+ token_hash,
+}: MagicLinkEmailProps) => (
+ <Html>
+ <Head />
+ <Preview>Log in with this magic link</Preview>
+ <Body style={main}>
+ <Container style={container}>
+ <Heading style={h1}>Login</Heading>
+ <Link
+ href={`${supabase_url}/auth/v1/verify?token=${token_hash}&type=${email_action_type}&redirect_to=${redirect_to}`}
+ target="_blank"
+ style={{
+ ...link,
+ display: 'block',
+ marginBottom: '16px',
+ }}
+ >
+ Click here to log in with this magic link
+ </Link>
+ <Text style={{ ...text, marginBottom: '14px' }}>
+ Or, copy and paste this temporary login code:
+ </Text>
+ <code style={code}>{token}</code>
+ <Text
+ style={{
+ ...text,
+ color: '#ababab',
+ marginTop: '14px',
+ marginBottom: '16px',
+ }}
+ >
+ If you didn&apos;t try to login, you can safely ignore this email.
+ </Text>
+ <Text style={footer}>
+ <Link
+ href="https://demo.vercel.store/"
+ target="_blank"
+ style={{ ...link, color: '#898989' }}
+ >
+ ACME Corp
+ </Link>
+ , the famouse demo corp.
+ </Text>
+ </Container>
+ </Body>
+ </Html>
+)
+
+export default MagicLinkEmail
+
+const main = {
+ backgroundColor: '#ffffff',
+}
+
+const container = {
+ paddingLeft: '12px',
+ paddingRight: '12px',
+ margin: '0 auto',
+}
+
+const h1 = {
+ color: '#333',
+ fontFamily:
+ "-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif",
+ fontSize: '24px',
+ fontWeight: 'bold',
+ margin: '40px 0',
+ padding: '0',
+}
+
+const link = {
+ color: '#2754C5',
+ fontFamily:
+ "-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif",
+ fontSize: '14px',
+ textDecoration: 'underline',
+}
+
+const text = {
+ color: '#333',
+ fontFamily:
+ "-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif",
+ fontSize: '14px',
+ margin: '24px 0',
+}
+
+const footer = {
+ color: '#898989',
+ fontFamily:
+ "-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif",
+ fontSize: '12px',
+ lineHeight: '22px',
+ marginTop: '12px',
+ marginBottom: '24px',
+}
+
+const code = {
+ display: 'inline-block',
+ padding: '16px 4.5%',
+ width: '90.5%',
+ backgroundColor: '#f4f4f4',
+ borderRadius: '5px',
+ border: '1px solid #eee',
+ color: '#333',
+}
+```
+
+<Admonition type="info">
+
+You can find a selection of React Email templates in the [React Email Eamples](https://react.email/examples).
+
+</Admonition>
+
+### 4. Deploy the Function
+
+Deploy function to Supabase:
+
+```bash
+supabase functions deploy send-email --no-verify-jwt
+```
+
+Note down the function URL, you will need it in the next step!
+
+### 5. Configure the Send Email Hook
+
+- Go to the [Auth Hooks](/dashboard/project/_/auth/hooks) section of the Supabase dashboard and create a new "Send Email hook".
+- Select HTTPS as the hook type.
+- Paste the function URL in the "URL" field.
+- Click "Generate Secret" to generate your webhook secret and note it down.
+- Click "Create" to save the hook configuration.
+
+Store these secrets in your `.env` file.
+
+```bash supabase/functions/.env
+RESEND_API_KEY=your_resend_api_key
+SEND_EMAIL_HOOK_SECRET=<base64_secret>
+```
+
+<Admonition type="info">
+
+You can generate the secret in the [Auth Hooks](/dashboard/project/_/auth/hooks) section of the Supabase dashboard. Make sure to remove the `v1,whsec_` prefix!
+
+</Admonition>
+
+Set the secrets from the `.env` file:
+
+```bash
+supabase secrets set --env-file supabase/functions/.env
+```
+
+That's it, now your Supabase Edge Function will be triggered anytime an Auth Email needs to be send to the user!
+
+## More Resources
+
+- [Send Email Hooks](/docs/guides/auth/auth-hooks/send-email-hook)
+- [Auth Hooks](/docs/guides/auth/auth-hooks)
diff --git a/examples/edge-functions/supabase/.env.local.example b/examples/edge-functions/supabase/.env.local.example
index d3f3566510b..d04ba136533 100644
--- a/examples/edge-functions/supabase/.env.local.example
+++ b/examples/edge-functions/supabase/.env.local.example
@@ -1,3 +1,7 @@
+# auth hook react email resend
+RESEND_API_KEY=your_resend_api_key
+SEND_EMAIL_HOOK_SECRET=base64_secret
+
# cloudflare-turnstile
CLOUDFLARE_TURNSTILE_SECRET_KEY=your_secret_key
diff --git a/examples/edge-functions/supabase/functions/auth-hook-react-email-resend/README.md b/examples/edge-functions/supabase/functions/auth-hook-react-email-resend/README.md
new file mode 100644
index 00000000000..2e2639f8d78
--- /dev/null
+++ b/examples/edge-functions/supabase/functions/auth-hook-react-email-resend/README.md
@@ -0,0 +1,12 @@
+# Supabase Auth Send Email Hook with React Email and Resend
+
+- [Supabase docs](https://supabase.com/docs/guides/auth/auth-hooks/send-email-hook?queryGroups=language&language=http)
+- [React Email Docs](https://react.email/docs/introduction)
+- [Create a Resend account](https://resend.com/)
+
+## Required Secrets
+
+```txt
+RESEND_API_KEY=<your_resend_api_key>
+SEND_EMAIL_HOOK_SECRET=<base64_secret>
+```
diff --git a/examples/edge-functions/supabase/functions/auth-hook-react-email-resend/_templates/magic-link.tsx b/examples/edge-functions/supabase/functions/auth-hook-react-email-resend/_templates/magic-link.tsx
new file mode 100644
index 00000000000..6f7cb81760b
--- /dev/null
+++ b/examples/edge-functions/supabase/functions/auth-hook-react-email-resend/_templates/magic-link.tsx
@@ -0,0 +1,130 @@
+import {
+ Body,
+ Container,
+ Head,
+ Heading,
+ Html,
+ Link,
+ Preview,
+ Text,
+} from 'npm:@react-email/[email protected]'
+import * as React from 'npm:[email protected]'
+
+interface MagicLinkEmailProps {
+ supabase_url: string
+ email_action_type: string
+ redirect_to: string
+ token_hash: string
+ token: string
+}
+
+export const MagicLinkEmail = ({
+ token,
+ supabase_url,
+ email_action_type,
+ redirect_to,
+ token_hash,
+}: MagicLinkEmailProps) => (
+ <Html>
+ <Head />
+ <Preview>Log in with this magic link</Preview>
+ <Body style={main}>
+ <Container style={container}>
+ <Heading style={h1}>Login</Heading>
+ <Link
+ href={`${supabase_url}/auth/v1/verify?token=${token_hash}&type=${email_action_type}&redirect_to=${redirect_to}`}
+ target="_blank"
+ style={{
+ ...link,
+ display: 'block',
+ marginBottom: '16px',
+ }}
+ >
+ Click here to log in with this magic link
+ </Link>
+ <Text style={{ ...text, marginBottom: '14px' }}>
+ Or, copy and paste this temporary login code:
+ </Text>
+ <code style={code}>{token}</code>
+ <Text
+ style={{
+ ...text,
+ color: '#ababab',
+ marginTop: '14px',
+ marginBottom: '16px',
+ }}
+ >
+ If you didn&apos;t try to login, you can safely ignore this email.
+ </Text>
+ <Text style={footer}>
+ <Link
+ href="https://demo.vercel.store/"
+ target="_blank"
+ style={{ ...link, color: '#898989' }}
+ >
+ ACME Corp
+ </Link>
+ , the famouse demo corp.
+ </Text>
+ </Container>
+ </Body>
+ </Html>
+)
+
+export default MagicLinkEmail
+
+const main = {
+ backgroundColor: '#ffffff',
+}
+
+const container = {
+ paddingLeft: '12px',
+ paddingRight: '12px',
+ margin: '0 auto',
+}
+
+const h1 = {
+ color: '#333',
+ fontFamily:
+ "-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif",
+ fontSize: '24px',
+ fontWeight: 'bold',
+ margin: '40px 0',
+ padding: '0',
+}
+
+const link = {
+ color: '#2754C5',
+ fontFamily:
+ "-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif",
+ fontSize: '14px',
+ textDecoration: 'underline',
+}
+
+const text = {
+ color: '#333',
+ fontFamily:
+ "-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif",
+ fontSize: '14px',
+ margin: '24px 0',
+}
+
+const footer = {
+ color: '#898989',
+ fontFamily:
+ "-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif",
+ fontSize: '12px',
+ lineHeight: '22px',
+ marginTop: '12px',
+ marginBottom: '24px',
+}
+
+const code = {
+ display: 'inline-block',
+ padding: '16px 4.5%',
+ width: '90.5%',
+ backgroundColor: '#f4f4f4',
+ borderRadius: '5px',
+ border: '1px solid #eee',
+ color: '#333',
+}
diff --git a/examples/edge-functions/supabase/functions/auth-hook-react-email-resend/index.ts b/examples/edge-functions/supabase/functions/auth-hook-react-email-resend/index.ts
new file mode 100644
index 00000000000..431245c3e58
--- /dev/null
+++ b/examples/edge-functions/supabase/functions/auth-hook-react-email-resend/index.ts
@@ -0,0 +1,78 @@
+import React from 'npm:[email protected]'
+import { Webhook } from 'https://esm.sh/[email protected]'
+import { Resend } from 'npm:[email protected]'
+import { renderAsync } from 'npm:@react-email/[email protected]'
+import { MagicLinkEmail } from './_templates/magic-link.tsx'
+
+const resend = new Resend(Deno.env.get('RESEND_API_KEY') as string)
+const hookSecret = Deno.env.get('SEND_EMAIL_HOOK_SECRET') as string
+
+Deno.serve(async (req) => {
+ if (req.method !== 'POST') {
+ return new Response('not allowed', { status: 400 })
+ }
+
+ const payload = await req.text()
+ const headers = Object.fromEntries(req.headers)
+ const wh = new Webhook(hookSecret)
+ try {
+ const {
+ user,
+ email_data: { token, token_hash, redirect_to, email_action_type },
+ } = wh.verify(payload, headers) as {
+ user: {
+ email: string
+ }
+ email_data: {
+ token: string
+ token_hash: string
+ redirect_to: string
+ email_action_type: string
+ site_url: string
+ token_new: string
+ token_hash_new: string
+ }
+ }
+
+ const html = await renderAsync(
+ React.createElement(MagicLinkEmail, {
+ supabase_url: Deno.env.get('SUPABASE_URL') ?? '',
+ token,
+ token_hash,
+ redirect_to,
+ email_action_type,
+ })
+ )
+
+ const { error } = await resend.emails.send({
+ from: 'welcome <[email protected]>',
+ to: [user.email],
+ subject: 'Supa Custom MagicLink!',
+ html,
+ })
+ if (error) {
+ throw error
+ }
+ } catch (error) {
+ console.log(error)
+ return new Response(
+ JSON.stringify({
+ error: {
+ http_code: error.code,
+ message: error.message,
+ },
+ }),
+ {
+ status: 401,
+ headers: { 'Content-Type': 'application/json' },
+ }
+ )
+ }
+
+ const responseHeaders = new Headers()
+ responseHeaders.set('Content-Type', 'application/json')
+ return new Response(JSON.stringify({}), {
+ status: 200,
+ headers: responseHeaders,
+ })
+})