Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: invitation

Aashish-Upadhyay-1012fd79d3d2cf9
diff --git a/components/emails/team-invitation.tsx b/components/emails/team-invitation.tsx
index ff0a11c1..46fba903 100644
--- a/components/emails/team-invitation.tsx
+++ b/components/emails/team-invitation.tsx
@@ -15,11 +15,13 @@ export default function TeamInvitation({
senderEmail,
teamName,
teamId,
+ token,
}: {
senderName: string;
senderEmail: string;
teamName: string;
teamId: string;
+ token: string;
}) {
return (
<Html>
@@ -35,9 +37,10 @@ export default function TeamInvitation({
Team Invitation
</Heading>
<Text className="text-sm leading-6 text-black">
- You are invited by ${senderName}-(${senderEmail} to join $
- {teamName}.) please click the link below to accept the invitation.
- \n\n{process.env.NEXTAUTH_URL}/api/teams/${teamId}/invite
+ You are invited by {senderName}-{senderEmail} to join
+ {teamName}. please click the link below to accept the invitation.
+ \n\n{process.env.NEXTAUTH_URL}/api/teams/{teamId}
+ /invite?teamName=${teamName}&token={token}
</Text>
</Container>
</Body>
diff --git a/components/teams/add-team-member-modal.tsx b/components/teams/add-team-member-modal.tsx
index 3787c9a3..a7f43771 100644
--- a/components/teams/add-team-member-modal.tsx
+++ b/components/teams/add-team-member-modal.tsx
@@ -57,6 +57,8 @@ export function AddTeamMembers({
return;
}
+ console.log("passes through!!");
+
await mutate(`/api/teams/${teamInfo?.currentTeam?.id}`);
toast.success("An invitation email has been sent!");
diff --git a/components/teams/delete-team-modal.tsx b/components/teams/delete-team-modal.tsx
index 51719ff3..052124b8 100644
--- a/components/teams/delete-team-modal.tsx
+++ b/components/teams/delete-team-modal.tsx
@@ -47,9 +47,9 @@ export function DeleteTeamModal({ children }: { children: React.ReactNode }) {
return;
}
- toast.success("Team deleted successfully!");
await mutate("/api/teams");
setLoading(false);
+ toast.success("Team deleted successfully!");
router.push("/documents");
};
diff --git a/lib/emails/send-teammate-invite.ts b/lib/emails/send-teammate-invite.ts
new file mode 100644
index 00000000..f93cc8e3
--- /dev/null
+++ b/lib/emails/send-teammate-invite.ts
@@ -0,0 +1,24 @@
+import TeamInvitation from "@/components/emails/team-invitation";
+import { sendEmail } from "@/lib/resend";
+
+export const sendTeammateInviteEmail = async ({
+ to,
+ senderName,
+ senderEmail,
+ teamName,
+ teamId,
+ token,
+}: {
+ to: string;
+ senderName: string;
+ senderEmail: string;
+ teamName: string;
+ teamId: string;
+ token: string;
+}) => {
+ await sendEmail({
+ to: to,
+ subject: "You are invited to join a Team",
+ react: TeamInvitation({ senderName, senderEmail, teamName, teamId, token }),
+ });
+};
diff --git a/pages/api/teams/[teamId]/invite.ts b/pages/api/teams/[teamId]/invite.ts
index 69489970..9a49bcb4 100644
--- a/pages/api/teams/[teamId]/invite.ts
+++ b/pages/api/teams/[teamId]/invite.ts
@@ -1,11 +1,11 @@
-import { sendEmail } from "@/lib/resend";
+import { randomUUID } from "crypto";
import { getServerSession } from "next-auth";
import { authOptions } from "../../auth/[...nextauth]";
import { NextApiRequest, NextApiResponse } from "next";
import prisma from "@/lib/prisma";
import { CustomUser } from "@/lib/types";
import { errorHanlder } from "@/lib/errorHandler";
-import TeamInvitation from "@/components/emails/team-invitation";
+import { sendTeammateInviteEmail } from "@/lib/emails/send-teammate-invite";
export default async function handle(
req: NextApiRequest,
@@ -52,19 +52,38 @@ export default async function handle(
return res.status(403).json("Only admins can send the invitation!");
}
+ const token = randomUUID().toString();
+ console.log("token", token);
+
+ const expiresAt = new Date();
+ expiresAt.setHours(expiresAt.getHours() + 24); // invitation expires in 24 hour
+ const invitation = await prisma.invitation.create({
+ data: {
+ email,
+ token,
+ expires: expiresAt,
+ teamId,
+ },
+ });
+
+ console.log(invitation);
+
// send invite email
const sender = session.user as CustomUser;
- sendTeamInviteEmail({
+ sendTeammateInviteEmail({
to: email,
senderName: sender.name || "",
senderEmail: sender.email || "",
teamName: team?.name || "",
teamId: team?.id || "",
+ token,
});
return res.status(200).json("Invitation send!");
} catch (error) {
+ console.log(error);
+
errorHanlder(error, res);
}
} else if (req.method === "GET") {
@@ -73,8 +92,10 @@ export default async function handle(
const { teamId } = req.query as { teamId: string };
+ const { token } = req.query as { token: string };
+
if (!session) {
- res.redirect(`/login?next=/api/teams/${teamId}/invite`);
+ res.redirect(`/login?next=/api/teams/${teamId}/invite?token=${token}`);
}
try {
@@ -90,7 +111,22 @@ export default async function handle(
return res.redirect(`/documents`);
}
- const team = await prisma.team.update({
+ const invitation = await prisma.invitation.findUnique({
+ where: {
+ token,
+ },
+ });
+
+ if (!invitation) {
+ return res.status(400).json("Invalid invitation token");
+ }
+
+ const currentTime = new Date();
+ if (currentTime > invitation.expires) {
+ return res.status(400).json("Invitation link has expired");
+ }
+
+ await prisma.team.update({
where: {
id: teamId,
},
@@ -102,7 +138,6 @@ export default async function handle(
},
},
});
- console.log(team);
return res.redirect("/documents");
} catch (error) {
@@ -111,23 +146,3 @@ export default async function handle(
}
}
}
-
-export const sendTeamInviteEmail = async ({
- to,
- senderName,
- senderEmail,
- teamName,
- teamId,
-}: {
- to: string;
- senderName: string;
- senderEmail: string;
- teamName: string;
- teamId: string;
-}) => {
- await sendEmail({
- to: to,
- subject: "You are invited to join a Team",
- react: TeamInvitation({ senderName, senderEmail, teamName, teamId }),
- });
-};
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 89a947b8..bdc3779e 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -57,18 +57,19 @@ model User {
}
model Team {
- id String @id @default(cuid())
+ id String @id @default(cuid())
name String
users UserTeam[]
documents Document[]
domains Domain[]
- plan String @default("free")
- stripeId String? @unique // Stripe subscription / customer ID
- subscriptionId String? @unique // Stripe subscription ID
+ invitations Invitation[]
+ plan String @default("free")
+ stripeId String? @unique // Stripe subscription / customer ID
+ subscriptionId String? @unique // Stripe subscription ID
startsAt DateTime? // Stripe subscription start date
endsAt DateTime? // Stripe subscription end date
- createdAt DateTime @default(now())
- updatedAt DateTime @updatedAt
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
}
enum Role {
@@ -189,3 +190,14 @@ model View {
viewerEmail String? // Email of the viewer if known
viewedAt DateTime @default(now())
}
+
+model Invitation {
+ email String
+ expires DateTime
+ teamId String
+ team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
+ createdAt DateTime @default(now())
+ token String @unique
+
+ @@unique([email])
+}