Skip to content

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

refactor: change token to generate custom id

Marc Seitz80823f5672ab
diff --git a/lib/id-helper.ts b/lib/id-helper.ts
index caf6ebe1..a880883e 100644
--- a/lib/id-helper.ts
+++ b/lib/id-helper.ts
@@ -34,4 +34,5 @@ export class IdGenerator<TPrefixes extends string> {
export const newId = new IdGenerator({
view: "view",
+ inv: "inv",
}).id;
diff --git a/pages/api/teams/[teamId]/invite.ts b/pages/api/teams/[teamId]/invite.ts
index 22b246c3..e27defdc 100644
--- a/pages/api/teams/[teamId]/invite.ts
+++ b/pages/api/teams/[teamId]/invite.ts
@@ -1,4 +1,3 @@
-import { randomUUID } from "crypto";
import { getServerSession } from "next-auth";
import { authOptions } from "../../auth/[...nextauth]";
import { NextApiRequest, NextApiResponse } from "next";
@@ -6,10 +5,11 @@ import prisma from "@/lib/prisma";
import { CustomUser } from "@/lib/types";
import { errorhandler } from "@/lib/errorHandler";
import { sendTeammateInviteEmail } from "@/lib/emails/send-teammate-invite";
+import { newId } from "@/lib/id-helper";
export default async function handle(
req: NextApiRequest,
- res: NextApiResponse
+ res: NextApiResponse,
) {
if (req.method === "POST") {
// POST /api/teams/:teamId/invite
@@ -46,13 +46,13 @@ export default async function handle(
const isUserAdmin = teamUsers?.some(
(user) =>
user.role === "ADMIN" &&
- user.userId === (session.user as CustomUser).id
+ user.userId === (session.user as CustomUser).id,
);
if (!isUserAdmin) {
return res.status(403).json("Only admins can send the invitation!");
}
- const token = randomUUID().toString();
+ const token = newId("inv");
const expiresAt = new Date();
expiresAt.setHours(expiresAt.getHours() + 24); // invitation expires in 24 hour
@@ -77,7 +77,7 @@ export default async function handle(
to: email,
});
- return res.status(200).json("Invitation send!");
+ return res.status(200).json("Invitation sent!");
} catch (error) {
errorhandler(error, res);
}
@@ -92,13 +92,16 @@ export default async function handle(
if (!session) {
res.redirect(`/login?next=/api/teams/${teamId}/invite?token=${token}`);
+ return;
}
+ const userId = (session.user as CustomUser).id;
+
try {
const userTeam = await prisma.userTeam.findFirst({
where: {
teamId,
- userId: (session?.user as CustomUser).id,
+ userId,
},
});
@@ -133,7 +136,7 @@ export default async function handle(
data: {
users: {
create: {
- userId: (session?.user as CustomUser).id,
+ userId,
},
},
},