Skip to content

Commit 7360b31

Browse files
committed
Use withAuth function for email endpoint
1 parent a00bd3c commit 7360b31

File tree

7 files changed

+7343
-4462
lines changed

7 files changed

+7343
-4462
lines changed

app/api/mail/route.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1-
import { Resend } from "resend"
2-
import { SelectEmailTemplate } from "@/components/email-templates"
3-
import { NextRequest, NextResponse } from "next/server"
1+
import { Resend } from "resend";
2+
import { SelectEmailTemplate } from "@/components/email-templates";
3+
import { NextRequest, NextResponse } from "next/server";
4+
import { withAuth } from "@/utils/withAuth";
45

5-
const resend = new Resend(process.env.RESEND_API_KEY!)
6+
const resend = new Resend(process.env.RESEND_API_KEY!);
67

7-
export async function POST(req: NextRequest) {
8-
const body = await req.json()
9-
const { to, subject, data } = body
10-
const EmailTemplate = SelectEmailTemplate(data)
8+
async function sendEmail(req: NextRequest) {
9+
const body = await req.json();
10+
const { to, subject, data } = body;
11+
const EmailTemplate = SelectEmailTemplate(data);
1112

1213
try {
1314
const { data, error } = await resend.emails.send({
1415
from: "Acme <[email protected]>", // Pear AI <[email protected]>
1516
to: [to],
1617
subject,
1718
react: EmailTemplate as JSX.Element,
18-
})
19+
});
1920

2021
if (error) {
21-
return NextResponse.json({ error, status: 500 })
22+
return NextResponse.json({ error, status: 500 });
2223
}
2324

24-
return NextResponse.json({ message: "Email sent!", data, status: 200 })
25+
return NextResponse.json({ message: "Email sent!", data, status: 200 });
2526
} catch (error) {
26-
return NextResponse.json({ error, status: 500 })
27+
return NextResponse.json({ error, status: 500 });
2728
}
2829
}
30+
31+
export const POST = withAuth(sendEmail);

components/email-templates/email-wrapper.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import {
1010
Link,
1111
Section,
1212
Heading,
13-
} from "@react-email/components"
14-
import * as React from "react"
15-
import { Tailwind } from "@react-email/tailwind"
13+
} from "@react-email/components";
14+
import * as React from "react";
15+
import { Tailwind } from "@react-email/tailwind";
1616

1717
type EmailWrapperProps = {
18-
children: React.ReactNode
19-
previewText: string
20-
dir?: "ltr" | "rtl"
21-
}
18+
children: React.ReactNode;
19+
previewText: string;
20+
dir?: "ltr" | "rtl";
21+
};
2222

2323
const EmailWrapper = ({ children, previewText, dir }: EmailWrapperProps) => {
2424
return (
@@ -65,7 +65,7 @@ const EmailWrapper = ({ children, previewText, dir }: EmailWrapperProps) => {
6565
</Body>
6666
</Tailwind>
6767
</Html>
68-
)
69-
}
68+
);
69+
};
7070

71-
export default EmailWrapper
71+
export default EmailWrapper;

components/email-templates/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import WelcomeTemplate from "./welcome-template"
1+
import WelcomeTemplate from "./welcome-template";
22

33
export enum EmailTemplates {
44
WELCOME = "WELCOME",
@@ -9,7 +9,7 @@ export function SelectEmailTemplate(data: any) {
99
case EmailTemplates.WELCOME:
1010
return WelcomeTemplate({
1111
userName: data.userName,
12-
})
12+
});
1313

1414
// more templates here
1515
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
import React from "react"
2-
import EmailWrapper from "./email-wrapper"
3-
import { Container, Heading, Text } from "@react-email/components"
1+
import React from "react";
2+
import EmailWrapper from "./email-wrapper";
3+
import { Container, Heading, Text } from "@react-email/components";
44

55
const WelcomeTemplate = ({ userName }: { userName: string }) => {
66
return (
77
<EmailWrapper previewText="Welcome to Pear AI" dir="ltr">
8-
<Container className="mx-auto -mt-3 max-w-[600px] rounded-xl bg-white px-8 py-5">
8+
<Container className="bg-white mx-auto -mt-3 max-w-[600px] rounded-xl px-8 py-5">
99
<Heading as="h1">Hi {userName},</Heading>
1010
<Heading as="h2">Welcome to Pear AI!</Heading>
1111
<Text>
1212
We are excited to have you onboard and look forward to helping you get
1313
the most out of our product.
1414
</Text>
1515
<Text>
16-
If you have any questions or need assistance, please don't hesitate to
16+
If you have any questions or need assistance, please dont hesitate to
1717
reach out to us.
1818
</Text>
1919
</Container>
2020
</EmailWrapper>
21-
)
22-
}
21+
);
22+
};
2323

24-
export default WelcomeTemplate
24+
export default WelcomeTemplate;

components/ui/header.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default async function Header() {
2727
{/* Site branding */}
2828
<div className="flex w-[14%] flex-row items-center space-x-2 md:w-[36%]">
2929
{/* Logo */}
30-
<Link className="-mt-0.5 dark:invert sm:mt-0" href="/">
30+
<Link className="-mt-0.5 sm:mt-0 dark:invert" href="/">
3131
<PearDarkLogo />
3232
</Link>
3333
</div>

0 commit comments

Comments
 (0)