Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

API Key Created email

Steven Teycd437112201c
diff --git a/apps/web/app/api/user/tokens/route.ts b/apps/web/app/api/user/tokens/route.ts
index f6ea67e4c5..efaeb71b19 100644
--- a/apps/web/app/api/user/tokens/route.ts
+++ b/apps/web/app/api/user/tokens/route.ts
@@ -1,6 +1,8 @@
import { hashToken, withSession } from "@/lib/auth";
import prisma from "@/lib/prisma";
import { nanoid } from "@dub/utils";
+import { sendEmail } from "emails";
+import APIKeyCreated from "emails/api-key-created";
import { NextResponse } from "next/server";
// GET /api/user/tokens – get all tokens for a specific user
@@ -37,14 +39,24 @@ export const POST = withSession(async ({ req, session }) => {
});
// take first 3 and last 4 characters of the key
const partialKey = `${token.slice(0, 3)}...${token.slice(-4)}`;
- await prisma.token.create({
- data: {
- name,
- hashedKey,
- partialKey,
- userId: session.user.id,
- },
- });
+ await Promise.all([
+ prisma.token.create({
+ data: {
+ name,
+ hashedKey,
+ partialKey,
+ userId: session.user.id,
+ },
+ }),
+ sendEmail({
+ email: session.user.email,
+ subject: "New API Key Created",
+ react: APIKeyCreated({
+ email: session.user.email,
+ apiKeyName: name,
+ }),
+ }),
+ ]);
return NextResponse.json({ token });
});
diff --git a/apps/web/emails/api-key-created.tsx b/apps/web/emails/api-key-created.tsx
new file mode 100644
index 0000000000..1ad26b1625
--- /dev/null
+++ b/apps/web/emails/api-key-created.tsx
@@ -0,0 +1,72 @@
+import { DUB_LOGO, formatDate } from "@dub/utils";
+import {
+ Body,
+ Container,
+ Head,
+ Heading,
+ Html,
+ Img,
+ Link,
+ Preview,
+ Section,
+ Tailwind,
+ Text,
+} from "@react-email/components";
+import Footer from "./components/footer";
+
+export default function APIKeyCreated({
+ email = "[email protected]",
+ apiKeyName = "Acme",
+}: {
+ email: string;
+ apiKeyName: string;
+}) {
+ return (
+ <Html>
+ <Head />
+ <Preview>New API Key Created</Preview>
+ <Tailwind>
+ <Body className="mx-auto my-auto bg-white font-sans">
+ <Container className="mx-auto my-10 max-w-[500px] rounded border border-solid border-gray-200 px-10 py-5">
+ <Section className="mt-8">
+ <Img
+ src={DUB_LOGO}
+ width="40"
+ height="40"
+ alt="Dub.co"
+ className="mx-auto my-0"
+ />
+ </Section>
+ <Heading className="mx-0 my-7 p-0 text-center text-xl font-semibold text-black">
+ New API Key Created
+ </Heading>
+ <Text className="text-sm leading-6 text-black">
+ You created a new API key for your Dub.co account with the name{" "}
+ <strong>"{apiKeyName}"</strong> on{" "}
+ {formatDate(new Date().toString())}.
+ </Text>
+ <Section className="my-8 text-center">
+ <Link
+ className="rounded-full bg-black px-6 py-3 text-center text-[12px] font-semibold text-white no-underline"
+ href="https://app.dub.co/settings/tokens"
+ >
+ View API Keys
+ </Link>
+ </Section>
+ <Text className="text-sm leading-6 text-black">
+ If you did not create this API key, you can{" "}
+ <Link
+ href="https://app.dub.co/settings/tokens"
+ className="text-black underline"
+ >
+ <strong>delete this key</strong>
+ </Link>{" "}
+ from your account.
+ </Text>
+ <Footer email={email} />
+ </Container>
+ </Body>
+ </Tailwind>
+ </Html>
+ );
+}