Skip to content

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

attempt fix: user unable to log in

Ashish Viradiya0c4b18b1d061
diff --git a/lib/swr/use-billing.ts b/lib/swr/use-billing.ts
index 98131aa2..e968a7df 100644
--- a/lib/swr/use-billing.ts
+++ b/lib/swr/use-billing.ts
@@ -2,6 +2,7 @@ import { useTeam } from "@/context/team-context";
import { PLAN_NAME_MAP } from "@/ee/stripe/constants";
import { SubscriptionDiscount } from "@/ee/stripe/functions/get-subscription-item";
import useSWR from "swr";
+import { useMemo } from "react";
import { fetcher } from "@/lib/utils";
@@ -64,15 +65,22 @@ interface PlanDetails {
}
function parsePlan(plan: BasePlan | PlanWithTrial | PlanWithOld): PlanDetails {
- if (!plan) return { plan: null, trial: null, old: false };
+ if (!plan || typeof plan !== 'string') {
+ return { plan: null, trial: null, old: false };
+ }
- // Split the plan on '+'
- const parts = plan.split("+");
- return {
- plan: parts[0] as BasePlan, // Always the base plan
- trial: parts.includes("drtrial") ? "drtrial" : null, // 'drtrial' if present, otherwise null
- old: parts.includes("old"), // true if 'old' is present, otherwise false
- };
+ try {
+ // Split the plan on '+'
+ const parts = plan.split("+");
+ return {
+ plan: parts[0] as BasePlan, // Always the base plan
+ trial: parts.includes("drtrial") ? "drtrial" : null, // 'drtrial' if present, otherwise null
+ old: parts.includes("old"), // true if 'old' is present, otherwise false
+ };
+ } catch (error) {
+ console.error("Error parsing plan:", error);
+ return { plan: null, trial: null, old: false };
+ }
}
export function usePlan({
@@ -86,15 +94,17 @@ export function usePlan({
error,
mutate,
} = useSWR<PlanResponse>(
- teamId &&
- `/api/teams/${teamId}/billing/plan${withDiscount ? "?withDiscount=true" : ""}`,
+ teamId ? `/api/teams/${teamId}/billing/plan${withDiscount ? "?withDiscount=true" : ""}` : null,
fetcher,
);
// Parse the plan using the parsing function
- const parsedPlan = plan
- ? parsePlan(plan.plan)
- : { plan: null, trial: null, old: false };
+ const parsedPlan = useMemo(() => {
+ if (!plan || !plan.plan) {
+ return { plan: null, trial: null, old: false };
+ }
+ return parsePlan(plan.plan);
+ }, [plan]);
return {
plan: parsedPlan.plan ?? "free",
@@ -119,7 +129,7 @@ export function usePlan({
isDatarooms:
parsedPlan.plan === "datarooms" || parsedPlan.plan === "datarooms-plus",
isDataroomsPlus: parsedPlan.plan === "datarooms-plus",
- loading: !plan && !error,
+ loading: !plan && !error && !!teamId, // Only show loading if we have a teamId but no data
error,
mutate,
};
diff --git a/lib/swr/use-teams.ts b/lib/swr/use-teams.ts
index e2a837d4..aef33e0c 100644
--- a/lib/swr/use-teams.ts
+++ b/lib/swr/use-teams.ts
@@ -11,7 +11,7 @@ export function useTeams() {
const { data: session } = useSession();
const { data: teams, isValidating } = useSWR<Team[]>(
- router.isReady && session && "/api/teams",
+ router.isReady && session ? "/api/teams" : null,
fetcher,
{
dedupingInterval: 20000,
@@ -20,7 +20,7 @@ export function useTeams() {
return {
teams,
- loading: teams ? false : true,
+ loading: !teams && (router.isReady && !!session),
isValidating,
};
}
diff --git a/pages/api/teams/[teamId]/billing/plan.ts b/pages/api/teams/[teamId]/billing/plan.ts
index 082d4fc5..eba540e6 100644
--- a/pages/api/teams/[teamId]/billing/plan.ts
+++ b/pages/api/teams/[teamId]/billing/plan.ts
@@ -48,7 +48,11 @@ export default async function handle(
},
});
- const isCustomer = !!team?.stripeId;
+ if (!team) {
+ return res.status(404).json({ error: "Team not found" });
+ }
+
+ const isCustomer = !!team.stripeId;
// calculate the plan cycle either yearly or monthly based on the startsAt and endsAt dates
let subscriptionCycle = "monthly";
@@ -82,13 +86,13 @@ export default async function handle(
}
return res.status(200).json({
- plan: team?.plan,
- startsAt: team?.startsAt,
- endsAt: team?.endsAt,
+ plan: team.plan,
+ startsAt: team.startsAt,
+ endsAt: team.endsAt,
isCustomer,
subscriptionCycle,
- pauseStartsAt: team?.pauseStartsAt,
- cancelledAt: team?.cancelledAt,
+ pauseStartsAt: team.pauseStartsAt,
+ cancelledAt: team.cancelledAt,
discount,
});
} catch (error) {