|
| 1 | +import { Resend } from 'resend' |
1 | 2 | import { z } from 'zod' |
2 | 3 | import { withQuery, withTrailingSlash } from 'ufo' |
3 | 4 |
|
4 | 5 | export default eventHandler(async (event) => { |
5 | | - // Get the email from body and validate its format |
6 | 6 | const { email } = await readValidatedBody(event, z.object({ |
7 | 7 | email: z.string().email().trim() |
8 | 8 | }).parse) |
9 | 9 |
|
10 | | - const listId = useRuntimeConfig(event).sendgrid.listId |
11 | | - if (!listId) { |
| 10 | + const { apiKey, audienceId } = useRuntimeConfig(event).resend |
| 11 | + if (!apiKey || !audienceId) { |
12 | 12 | throw createError({ |
13 | 13 | statusCode: 500, |
14 | | - message: 'Missing NUXT_SENDGRID_LIST_ID env variable' |
| 14 | + message: 'Missing Resend configuration' |
15 | 15 | }) |
16 | 16 | } |
17 | 17 |
|
18 | | - // Check if already in contact list |
19 | | - await sendgrid.searchContact(event, email) |
20 | | - .catch((err) => { |
21 | | - if (err.statusCode !== 404) { |
22 | | - throw createError({ |
23 | | - message: err?.data?.errors?.[0]?.message || 'Sorry, we could not verify our contact list.', |
24 | | - statusCode: 400 |
25 | | - }) |
26 | | - } |
27 | | - }).then((res = {}) => { |
28 | | - if (res?.[email]?.contact?.list_ids?.includes(listId)) { |
29 | | - throw createError({ |
30 | | - message: 'You are already subscribed to the newsletter ❤️', |
31 | | - statusCode: 400 |
32 | | - }) |
33 | | - } |
34 | | - }) |
35 | | - // Add to global contacts first |
36 | | - await sendgrid.addContact(event, email) |
37 | | - .catch((err) => { |
38 | | - throw createError({ |
39 | | - message: err?.data?.errors?.[0]?.message || 'The email is invalid.', |
40 | | - statusCode: 400 |
41 | | - }) |
| 18 | + const resend = new Resend(apiKey) |
| 19 | + |
| 20 | + // Check if the user is already subscribed |
| 21 | + const { data: contact } = await resend.contacts.get({ audienceId, id: email }) |
| 22 | + if (contact && !contact.unsubscribed) { |
| 23 | + throw createError({ |
| 24 | + message: 'You are already subscribed to the newsletter ❤️', |
| 25 | + statusCode: 400 |
42 | 26 | }) |
| 27 | + } |
43 | 28 |
|
44 | | - // Send email to confirm registration |
45 | 29 | const confirmation = generateConfirmation(event, email) |
46 | | - const confirmationURL = withQuery(withTrailingSlash(getHeader(event, 'origin') || 'https://nuxt.com'), { email, confirmation }) |
| 30 | + const origin = import.meta.dev ? getRequestURL(event).origin : 'https://nuxt.com' |
| 31 | + const confirmationURL = withQuery(withTrailingSlash(origin), { email, confirmation }) |
47 | 32 |
|
48 | | - await sendgrid.sendEmail(event, { |
49 | | - personalizations: [ |
50 | | - { |
51 | | - to: [{ email }] |
| 33 | + const { error } = await resend.emails.send({ |
| 34 | + from: 'Nuxt Team <team@newsletter.nuxt.com>', |
| 35 | + to: email, |
| 36 | + subject: 'Confirm your email address', |
| 37 | + template: { |
| 38 | + id: 'confirm-newsletter', |
| 39 | + variables: { |
| 40 | + confirmationURL |
52 | 41 | } |
53 | | - ], |
54 | | - from: { |
55 | | - name: 'Nuxt Team', |
56 | | - email: 'team@nuxt.com' |
57 | | - }, |
58 | | - subject: 'Confirm your subscription to the Nuxt newsletter', |
59 | | - content: [ |
60 | | - { |
61 | | - type: 'text/html', |
62 | | - value: `Hello,<br><br>Thank you for subscribing to the Nuxt newsletter.<br>Please complete and confirm your subscription by <a href="${confirmationURL}">clicking here</a>.<br><br>Have a wonderful day.<br>The <a href="https://nuxt.com">Nuxt</a> team.` |
63 | | - } |
64 | | - ] |
| 42 | + } |
65 | 43 | }) |
66 | 44 |
|
| 45 | + if (error) { |
| 46 | + throw createError({ |
| 47 | + statusCode: 400, |
| 48 | + message: error.message || 'Failed to send confirmation email. Please try again.' |
| 49 | + }) |
| 50 | + } |
| 51 | + |
67 | 52 | return { ok: true } |
68 | 53 | }) |
0 commit comments