Skip to content

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

missing team scopes

Aashish-Upadhyay-101ef7f7040d4f0
diff --git a/components/PDFViewer.tsx b/components/PDFViewer.tsx
index fdf6e967..a15c8d9f 100644
--- a/components/PDFViewer.tsx
+++ b/components/PDFViewer.tsx
@@ -2,6 +2,7 @@ import { ChevronLeftIcon, ChevronRightIcon } from "@heroicons/react/20/solid";
import { useEffect, useRef, useState } from "react";
import { Document, Page, pdfjs } from "react-pdf";
import { Download } from "lucide-react";
+import { useTeam } from "@/context/team-context";
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;
@@ -117,7 +118,9 @@ export default function PDFViewer(props: any) {
}
async function updateNumPages(numPages: number) {
- await fetch(`/api/documents/update`, {
+ const teamInfo = useTeam();
+
+ await fetch(`/api/teams/${teamInfo?.currentTeam?.id}/documents/update`, {
method: "POST",
body: JSON.stringify({
documentId: props.documentId,
@@ -140,24 +143,20 @@ export default function PDFViewer(props: any) {
<div
hidden={loading}
style={{ height: "calc(100vh - 64px)" }}
- className="flex items-center"
- >
+ className="flex items-center">
<div
- className={`flex items-center justify-between w-full absolute z-10 px-2`}
- >
+ className={`flex items-center justify-between w-full absolute z-10 px-2`}>
<button
onClick={goToPreviousPage}
disabled={pageNumber <= 1}
- className="relative h-[calc(100vh - 64px)] px-2 py-24 text-gray-400 hover:text-gray-50 focus:z-20"
- >
+ className="relative h-[calc(100vh - 64px)] px-2 py-24 text-gray-400 hover:text-gray-50 focus:z-20">
<span className="sr-only">Previous</span>
<ChevronLeftIcon className="h-10 w-10" aria-hidden="true" />
</button>
<button
onClick={goToNextPage}
disabled={pageNumber >= numPages!}
- className="relative h-[calc(100vh - 64px)] px-2 py-24 text-gray-400 hover:text-gray-50 focus:z-20"
- >
+ className="relative h-[calc(100vh - 64px)] px-2 py-24 text-gray-400 hover:text-gray-50 focus:z-20">
<span className="sr-only">Next</span>
<ChevronRightIcon className="h-10 w-10" aria-hidden="true" />
</button>
@@ -169,8 +168,7 @@ export default function PDFViewer(props: any) {
onLoadSuccess={onDocumentLoadSuccess}
options={options}
renderMode="canvas"
- className=""
- >
+ className="">
<Page
className=""
key={pageNumber}
diff --git a/components/domains/domain-card.tsx b/components/domains/domain-card.tsx
index 1beff4d2..71e08c62 100644
--- a/components/domains/domain-card.tsx
+++ b/components/domains/domain-card.tsx
@@ -9,6 +9,7 @@ import LoadingSpinner from "../ui/loading-spinner";
import ExternalLink from "../shared/icons/external-link";
import { Button } from "../ui/button";
import { useState } from "react";
+import { useTeam } from "@/context/team-context";
export default function DomainCard({
domain,
@@ -18,15 +19,19 @@ export default function DomainCard({
onDelete: (deletedDomain: string) => void;
}) {
const { status, loading } = useDomainStatus({ domain });
- const [deleting, setDeleting] = useState<boolean>(false)
+ const [deleting, setDeleting] = useState<boolean>(false);
+ const teamInfo = useTeam();
const handleDelete = async () => {
// console.log("Deleting domain...", domain);
- setDeleting(true)
- const response = await fetch(`/api/domains/${domain}`, {
- method: "DELETE",
- });
+ setDeleting(true);
+ const response = await fetch(
+ `/api/teams/${teamInfo?.currentTeam?.id}/domains/${domain}`,
+ {
+ method: "DELETE",
+ }
+ );
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
@@ -46,8 +51,7 @@ export default function DomainCard({
href={`http://${domain}`}
target="_blank"
rel="noreferrer"
- className="flex items-center space-x-2"
- >
+ className="flex items-center space-x-2">
<p className="flex items-center text-xl font-semibold">
{domain}
</p>
@@ -60,16 +64,16 @@ export default function DomainCard({
className="bg-gray-300 hover:bg-gray-300/80 dark:bg-gray-700 hover:dark:bg-gray-700/80"
loading={loading}
onClick={() => {
- mutate(`/api/domains/${domain}/verify`);
- }}
- >
+ mutate(
+ `/api/teams/${teamInfo?.currentTeam?.id}/domains/${domain}/verify`
+ );
+ }}>
Refresh
</Button>
<Button
variant="destructive"
onClick={handleDelete}
- loading={deleting}
- >
+ loading={deleting}>
Delete
</Button>
</div>
diff --git a/components/domains/use-domain-status.ts b/components/domains/use-domain-status.ts
index 9f522323..3b0c5f62 100644
--- a/components/domains/use-domain-status.ts
+++ b/components/domains/use-domain-status.ts
@@ -1,13 +1,16 @@
import useSWR from "swr";
import { fetcher } from "@/lib/utils";
import { DomainResponse, DomainVerificationStatusProps } from "@/lib/types";
+import { useTeam } from "@/context/team-context";
export function useDomainStatus({ domain }: { domain: string }) {
+ const teamInfo = useTeam();
+
const { data, isValidating } = useSWR<{
status: DomainVerificationStatusProps;
domainJson: DomainResponse & { error: { code: string; message: string } };
}>(
- `/api/domains/${domain}/verify`,
+ `/api/teams/${teamInfo?.currentTeam?.id}/domains/${domain}/verify`,
(input, init) => fetcher(input, { ...init, cache: "no-store" }),
{
revalidateOnMount: true,
diff --git a/components/emails/team-invitation.tsx b/components/emails/team-invitation.tsx
index 46fba903..c108d9d2 100644
--- a/components/emails/team-invitation.tsx
+++ b/components/emails/team-invitation.tsx
@@ -16,12 +16,14 @@ export default function TeamInvitation({
teamName,
teamId,
token,
+ to,
}: {
senderName: string;
senderEmail: string;
teamName: string;
teamId: string;
token: string;
+ to: string;
}) {
return (
<Html>
@@ -40,7 +42,7 @@ export default function TeamInvitation({
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}
+ /invite?email=${to}&token={token}
</Text>
</Container>
</Body>
diff --git a/components/links/link-sheet/domain-section.tsx b/components/links/link-sheet/domain-section.tsx
index b2086ddd..04029c49 100644
--- a/components/links/link-sheet/domain-section.tsx
+++ b/components/links/link-sheet/domain-section.tsx
@@ -7,6 +7,7 @@ import { AddDomainModal } from "@/components/domains/add-domain-modal";
import { Button } from "@/components/ui/button";
import { mutate } from "swr";
import Link from "next/link";
+import { useTeam } from "@/context/team-context";
export default function DomainSection({
data,
@@ -18,6 +19,7 @@ export default function DomainSection({
domains?: Domain[];
}) {
const [isModalOpen, setModalOpen] = useState(false);
+ const teamInfo = useTeam();
const handleDomainChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
const value = event.target.value;
@@ -32,11 +34,11 @@ export default function DomainSection({
};
const handleSelectFocus = () => {
- // Assuming your fetcher key for domains is '/api/domains'
- mutate("/api/domains");
+ // Assuming your fetcher key for domains is '/api/teams/:teamId/domains'
+ mutate(`/api/teams/${teamInfo?.currentTeam?.id}/domains`);
};
- const currentDomain = domains?.find(domain => domain.slug === data.domain);
+ const currentDomain = domains?.find((domain) => domain.slug === data.domain);
const isDomainVerified = currentDomain?.verified;
return (
@@ -52,8 +54,7 @@ export default function DomainSection({
data.domain && data.domain !== "papermark.io"
? ""
: "rounded-r-md border-r-1"
- )}
- >
+ )}>
<option key="papermark.io" value="papermark.io">
papermark.io
</option>
@@ -96,14 +97,17 @@ export default function DomainSection({
{data.domain && data.domain !== "papermark.io" && !isDomainVerified ? (
<div className="text-sm text-red-500 mt-4">
- Your domain is not verified yet! <Link className="underline hover:text-red-500/80" href="/settings/domains" target="_blank">Verify now</Link>
+ Your domain is not verified yet!{" "}
+ <Link
+ className="underline hover:text-red-500/80"
+ href="/settings/domains"
+ target="_blank">
+ Verify now
+ </Link>
</div>
) : null}
- <AddDomainModal
- open={isModalOpen}
- setOpen={setModalOpen}
- />
+ <AddDomainModal open={isModalOpen} setOpen={setModalOpen} />
</>
);
}
diff --git a/components/links/link-sheet/index.tsx b/components/links/link-sheet/index.tsx
index 1ce5a9bf..13cae173 100644
--- a/components/links/link-sheet/index.tsx
+++ b/components/links/link-sheet/index.tsx
@@ -23,6 +23,7 @@ import { UpgradePlanModal } from "@/components/billing/upgrade-plan-modal";
import Link from "next/link";
import DomainSection from "./domain-section";
import AllowDownloadSection from "./allow-download-section";
+import { useTeam } from "@/context/team-context";
export const DEFAULT_LINK_PROPS = {
id: null,
@@ -57,6 +58,7 @@ export default function LinkSheet({
}) {
const { links } = useDocumentLinks();
const { domains } = useDomains();
+ const teamInfo = useTeam();
const [data, setData] = useState<DEFAULT_LINK_TYPE>(DEFAULT_LINK_PROPS);
const [isLoading, setIsLoading] = useState<boolean>(false);
@@ -106,7 +108,9 @@ export default function LinkSheet({
setIsOpen(false);
// Update the link in the list of links
mutate(
- `/api/documents/${encodeURIComponent(documentId)}/links`,
+ `/api/teams/${teamInfo?.currentTeam?.id}/documents/${encodeURIComponent(
+ documentId
+ )}/links`,
(links || []).map((link) =>
link.id === currentLink.id ? returnedLink : link
),
@@ -117,7 +121,9 @@ export default function LinkSheet({
setIsOpen(false);
// Add the new link to the list of links
mutate(
- `/api/documents/${encodeURIComponent(documentId)}/links`,
+ `/api/teams/${teamInfo?.currentTeam?.id}/documents/${encodeURIComponent(
+ documentId
+ )}/links`,
[...(links || []), returnedLink],
false
);
diff --git a/components/links/links-table.tsx b/components/links/links-table.tsx
index 8b482c2d..fcd108a7 100644
--- a/components/links/links-table.tsx
+++ b/components/links/links-table.tsx
@@ -38,11 +38,13 @@ import { mutate } from "swr";
import { toast } from "sonner";
import { useRouter } from "next/router";
import { usePlan } from "@/lib/swr/use-billing";
+import { useTeam } from "@/context/team-context";
export default function LinksTable() {
const { links } = useDocumentLinks();
const router = useRouter();
- const { plan } = usePlan()
+ const { plan } = usePlan();
+ const teamInfo = useTeam();
const [isLoading, setIsLoading] = useState<boolean>(false);
const [isLinkSheetVisible, setIsLinkSheetVisible] = useState<boolean>(false);
@@ -95,7 +97,9 @@ export default function LinksTable() {
// Update the archived link in the list of links
mutate(
- `/api/documents/${encodeURIComponent(documentId)}/links`,
+ `/api/teams/${teamInfo?.currentTeam?.id}/documents/${encodeURIComponent(
+ documentId
+ )}/links`,
(links || []).map((link) => (link.id === linkId ? archivedLink : link)),
false
);
@@ -112,7 +116,7 @@ export default function LinksTable() {
? links.filter((link) => link.isArchived).length
: 0;
- const hasFreePlan = plan && plan.plan === "free"
+ const hasFreePlan = plan && plan.plan === "free";
return (
<>
@@ -156,8 +160,7 @@ export default function LinksTable() {
link.domainId && hasFreePlan
? "bg-destructive hover:bg-red-700 hover:dark:bg-red-200"
: "bg-secondary hover:bg-emerald-700 hover:dark:bg-emerald-200"
- )}
- >
+ )}>
<div className="whitespace-nowrap hidden sm:flex text-sm group-hover/cell:hidden">
{link.domainId
? `https://${link.domainSlug}/${link.slug}`
@@ -170,8 +173,7 @@ export default function LinksTable() {
onClick={() =>
router.push("/settings/billing")
}
- title="Upgrade to activate link"
- >
+ title="Upgrade to activate link">
Upgrade{" "}
<span className="hidden sm:inline-flex">
to activate link
@@ -191,8 +193,7 @@ export default function LinksTable() {
`https://${process.env.NEXT_PUBLIC_ROOT_DOMAIN}/view/${link.id}`
)
}
- title="Copy to clipboard"
- >
+ title="Copy to clipboard">
Copy{" "}
<span className="hidden sm:inline-flex">
to Clipboard
@@ -220,8 +221,7 @@ export default function LinksTable() {
<time
dateTime={new Date(
link.views[0].viewedAt
- ).toISOString()}
- >
+ ).toISOString()}>
{timeAgo(link.views[0].viewedAt)}
</time>
) : (
@@ -239,8 +239,7 @@ export default function LinksTable() {
<DropdownMenuContent align="end">
<DropdownMenuLabel>Actions</DropdownMenuLabel>
<DropdownMenuItem
- onClick={() => handleEditLink(link)}
- >
+ onClick={() => handleEditLink(link)}>
Edit Link
</DropdownMenuItem>
<DropdownMenuSeparator />
@@ -252,8 +251,7 @@ export default function LinksTable() {
link.documentId,
link.isArchived
)
- }
- >
+ }>
Archive
</DropdownMenuItem>
</DropdownMenuContent>
@@ -300,8 +298,7 @@ export default function LinksTable() {
<Button
variant="secondary"
size="sm"
- className="text-gray-400 mx-auto flex items-center gap-x-1 h-8 justify-center mt-4 [&[data-state=open]>svg.chevron]:rotate-180"
- >
+ className="text-gray-400 mx-auto flex items-center gap-x-1 h-8 justify-center mt-4 [&[data-state=open]>svg.chevron]:rotate-180">
{archivedLinksCount} Archived Links
<ChevronDown className="h-4 w-4 text-gray-400 transition-transform duration-200 chevron" />
</Button>
@@ -363,8 +360,7 @@ export default function LinksTable() {
<time
dateTime={new Date(
link.views[0].viewedAt
- ).toISOString()}
- >
+ ).toISOString()}>
{timeAgo(link.views[0].viewedAt)}
</time>
) : (
@@ -376,8 +372,7 @@ export default function LinksTable() {
<DropdownMenuTrigger asChild>
<Button
variant="ghost"
- className="h-8 w-8 p-0"
- >
+ className="h-8 w-8 p-0">
<span className="sr-only">
Open menu
</span>
@@ -398,8 +393,7 @@ export default function LinksTable() {
link.documentId,
link.isArchived
)
- }
- >
+ }>
Reactivate
</DropdownMenuItem>
</DropdownMenuContent>
diff --git a/lib/emails/send-teammate-invite.ts b/lib/emails/send-teammate-invite.ts
index f93cc8e3..3d6c0a78 100644
--- a/lib/emails/send-teammate-invite.ts
+++ b/lib/emails/send-teammate-invite.ts
@@ -2,23 +2,30 @@ import TeamInvitation from "@/components/emails/team-invitation";
import { sendEmail } from "@/lib/resend";
export const sendTeammateInviteEmail = async ({
- to,
senderName,
senderEmail,
teamName,
teamId,
token,
+ to,
}: {
- to: string;
senderName: string;
senderEmail: string;
teamName: string;
teamId: string;
token: string;
+ to: string;
}) => {
await sendEmail({
to: to,
subject: "You are invited to join a Team",
- react: TeamInvitation({ senderName, senderEmail, teamName, teamId, token }),
+ react: TeamInvitation({
+ senderName,
+ senderEmail,
+ teamName,
+ teamId,
+ token,
+ to,
+ }),
});
};
diff --git a/lib/swr/use-billing.ts b/lib/swr/use-billing.ts
index 881f088c..8f75400a 100644
--- a/lib/swr/use-billing.ts
+++ b/lib/swr/use-billing.ts
@@ -38,8 +38,10 @@ interface PlanResponse {
}
export function usePlan() {
+ const teamInfo = useTeam();
+
const { data: plan, error } = useSWR<PlanResponse>(
- `/api/billing/plan`,
+ `/api/teams/${teamInfo?.currentTeam?.id}/billing/plan`,
fetcher,
{
dedupingInterval: 60000,
diff --git a/pages/api/teams/[teamId]/billing/manage.ts b/pages/api/teams/[teamId]/billing/manage.ts
index ebcec010..5b870caf 100644
--- a/pages/api/teams/[teamId]/billing/manage.ts
+++ b/pages/api/teams/[teamId]/billing/manage.ts
@@ -11,7 +11,7 @@ export default async function handle(
res: NextApiResponse
) {
if (req.method === "POST") {
- // POST /api/billing/manage – manage a user's subscription
+ // POST /api/teams/:teamId/billing/manage – manage a user's subscription
const session = await getServerSession(req, res, authOptions);
if (!session) {
res.status(401).end("Unauthorized");
diff --git a/pages/api/teams/[teamId]/billing/plan.ts b/pages/api/teams/[teamId]/billing/plan.ts
index 137f9f98..31061802 100644
--- a/pages/api/teams/[teamId]/billing/plan.ts
+++ b/pages/api/teams/[teamId]/billing/plan.ts
@@ -2,6 +2,7 @@ import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
import prisma from "@/lib/prisma";
import { authOptions } from "../../../auth/[...nextauth]";
+import { errorHanlder } from "@/lib/errorHandler";
export default async function handle(
req: NextApiRequest,
@@ -16,17 +17,21 @@ export default async function handle(
const { teamId } = req.query as { teamId: string };
- const team = await prisma.team.findUnique({
- where: {
- id: teamId,
- },
- select: {
- plan: true,
- },
- });
+ try {
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ },
+ select: {
+ plan: true,
+ },
+ });
- // console.log("Domains from GET", domains)
- return res.status(200).json(team);
+ // console.log("Domains from GET", domains)
+ return res.status(200).json(team);
+ } catch (error) {
+ errorHanlder(error, res);
+ }
} else {
// We only allow GET and POST requests
res.setHeader("Allow", ["GET"]);
diff --git a/pages/api/teams/[teamId]/invite.ts b/pages/api/teams/[teamId]/invite.ts
index 357d05a8..d4753566 100644
--- a/pages/api/teams/[teamId]/invite.ts
+++ b/pages/api/teams/[teamId]/invite.ts
@@ -69,12 +69,12 @@ export default async function handle(
const sender = session.user as CustomUser;
sendTeammateInviteEmail({
- to: email,
senderName: sender.name || "",
senderEmail: sender.email || "",
teamName: team?.name || "",
teamId: team?.id || "",
token,
+ to: email,
});
return res.status(200).json("Invitation send!");
@@ -85,14 +85,20 @@ export default async function handle(
// GET /api/teams/:teamId/invite
const session = await getServerSession(req, res, authOptions);
- const { teamId } = req.query as { teamId: string };
-
- const { token } = req.query as { token: string };
+ const { token, teamId, email } = req.query as {
+ token: string;
+ teamId: string;
+ email: string;
+ };
if (!session) {
res.redirect(`/login?next=/api/teams/${teamId}/invite?token=${token}`);
}
+ if (email !== session?.user?.email) {
+ return res.status(400).json("You are not invited to the team");
+ }
+
try {
const userTeam = await prisma.userTeam.findFirst({
where: {
diff --git a/pages/documents/[id].tsx b/pages/documents/[id].tsx
index d371254a..644bfa59 100644
--- a/pages/documents/[id].tsx
+++ b/pages/documents/[id].tsx
@@ -95,9 +95,12 @@ export default function DocumentPage() {
return;
}
- const response = await fetch(`/api/documents/${documentId}`, {
- method: "DELETE",
- });
+ const response = await fetch(
+ `/api/teams/${teamInfo?.currentTeam?.id}/documents/${documentId}`,
+ {
+ method: "DELETE",
+ }
+ );
if (response.ok) {
setIsFirstClick(false);
diff --git a/pages/settings/billing.tsx b/pages/settings/billing.tsx
index f9c32bc2..59ab42f4 100644
--- a/pages/settings/billing.tsx
+++ b/pages/settings/billing.tsx
@@ -170,9 +170,12 @@ export default function Billing() {
<Button
onClick={() => {
setClicked(true);
- fetch(`/api/billing/manage`, {
- method: "POST",
- })
+ fetch(
+ `/api/teams/${teamInfo?.currentTeam?.id}/billing/manage`,
+ {
+ method: "POST",
+ }
+ )
.then(async (res) => {
const url = await res.json();
router.push(url);
diff --git a/pages/settings/domains.tsx b/pages/settings/domains.tsx
index 906b9051..d338a49f 100644
--- a/pages/settings/domains.tsx
+++ b/pages/settings/domains.tsx
@@ -3,49 +3,66 @@ import DomainCard from "@/components/domains/domain-card";
import AppLayout from "@/components/layouts/app";
import Navbar from "@/components/settings/navbar";
import { Button } from "@/components/ui/button";
+import { useTeam } from "@/context/team-context";
import { useDomains } from "@/lib/swr/use-domains";
import { useState } from "react";
import { mutate } from "swr";
export default function Domains() {
const { domains } = useDomains();
+ const teamInfo = useTeam();
+
+ console.log("domains", domains);
+
const [open, setOpen] = useState<boolean>(false);
-
+
const handleDomainDeletion = (deletedDomain: string) => {
mutate(
- "/api/domains",
+ `/api/teams/${teamInfo?.currentTeam?.id}/domains`,
domains?.filter((domain) => domain.slug !== deletedDomain),
false
);
};
const handleDomainAddition = (newDomain: string) => {
- mutate("/api/domains", [...(domains || []), newDomain], false);
- }
+ mutate(
+ `/api/teams/${teamInfo?.currentTeam?.id}/domains`,
+ [...(domains || []), newDomain],
+ false
+ );
+ };
return (
<AppLayout>
- <Navbar current="Domains"/>
+ <Navbar current="Domains" />
<div className="p-4 sm:p-4 sm:m-4">
<div className="flex items-center justify-between mb-4 md:mb-8 lg:mb-12">
<div className="space-y-1">
<h3 className="text-2xl text-foreground font-semibold tracking-tight">
Domains
</h3>
- <p className="text-sm text-muted-foreground">Manage your custom domain for document sharing</p>
+ <p className="text-sm text-muted-foreground">
+ Manage your custom domain for document sharing
+ </p>
</div>
<ul className="flex items-center justify-between gap-4">
- <AddDomainModal open={open} setOpen={setOpen} onAddition={handleDomainAddition}>
+ <AddDomainModal
+ open={open}
+ setOpen={setOpen}
+ onAddition={handleDomainAddition}>
<Button>Add Domain</Button>
</AddDomainModal>
</ul>
</div>
- {(domains && domains.length !== 0) ? (
+ {domains && domains.length !== 0 ? (
<div>
<ul>
{domains.map((domain, index) => (
<li key={index} className="mt-4">
- <DomainCard domain={domain.slug} onDelete={handleDomainDeletion} />
+ <DomainCard
+ domain={domain.slug}
+ onDelete={handleDomainDeletion}
+ />
</li>
))}
</ul>
@@ -55,5 +72,3 @@ export default function Domains() {
</AppLayout>
);
}
-
-