Skip to content

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

feat: add swr for getting user's plan

Marc Seitzae96f05f0669
diff --git a/lib/swr/use-billing.ts b/lib/swr/use-billing.ts
index 87af5801..0fd5db93 100644
--- a/lib/swr/use-billing.ts
+++ b/lib/swr/use-billing.ts
@@ -30,3 +30,23 @@ export function useBilling() {
loading: !data && !error,
};
}
+
+interface PlanResponse {
+ plan: string;
+}
+
+export function usePlan() {
+ const { data: plan, error } = useSWR<PlanResponse>(
+ `/api/billing/plan`,
+ fetcher,
+ {
+ dedupingInterval: 60000,
+ }
+ );
+
+ return {
+ plan,
+ loading: !plan && !error,
+ error,
+ };
+}
diff --git a/pages/api/billing/plan.ts b/pages/api/billing/plan.ts
new file mode 100644
index 00000000..8d41e97b
--- /dev/null
+++ b/pages/api/billing/plan.ts
@@ -0,0 +1,34 @@
+import { NextApiRequest, NextApiResponse } from "next";
+import { getServerSession } from "next-auth/next";
+import prisma from "@/lib/prisma";
+import { authOptions } from "../auth/[...nextauth]";
+import { CustomUser } from "@/lib/types";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse
+) {
+ if (req.method === "GET") {
+ // GET /api/billing/plan
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const user = await prisma.user.findUnique({
+ where: {
+ id: (session.user as CustomUser).id,
+ },
+ select: {
+ plan: true,
+ },
+ });
+
+ // console.log("Domains from GET", domains)
+ return res.status(200).json(user);
+ } else {
+ // We only allow GET and POST requests
+ res.setHeader("Allow", ["GET"]);
+ return res.status(405).end(`Method ${req.method} Not Allowed`);
+ }
+}