From 0f43c00aa17f20692ad2ee4123d35df64af18cb6 Mon Sep 17 00:00:00 2001 From: msukkari Date: Thu, 13 Feb 2025 15:11:48 -0800 Subject: [PATCH] add auth and membership check to fetchSubscription --- packages/web/src/actions.ts | 45 +++++++++++++----------- packages/web/src/app/[domain]/layout.tsx | 2 +- packages/web/src/app/redeem/page.tsx | 32 ----------------- 3 files changed, 25 insertions(+), 54 deletions(-) diff --git a/packages/web/src/actions.ts b/packages/web/src/actions.ts index 96c810ae..4b10de89 100644 --- a/packages/web/src/actions.ts +++ b/packages/web/src/actions.ts @@ -329,9 +329,9 @@ export const redeemInvite = async (invite: Invite, userId: string): Promise<{ su return notFound(); } - // Incrememnt the seat count. We check if the subscription is valid in the redeem page so we return an error if that's not the case here + // Incrememnt the seat count if (org.stripeCustomerId) { - const subscription = await fetchSubscription(org.id); + const subscription = await fetchSubscription(org.domain); if (isServiceError(subscription)) { return orgInvalidSubscription(); } @@ -422,6 +422,7 @@ export const setupInitialStripeCustomer = async (name: string, domain: string) = const origin = (await headers()).get('origin') + @nocheckin const test_clock = await stripe.testHelpers.testClocks.create({ frozen_time: Math.floor(Date.now() / 1000) }) @@ -541,26 +542,28 @@ export const getCustomerPortalSessionLink = async (domain: string): Promise => + withAuth((session) => + withOrgMembership(session, domain, async (orgId) => { + const org = await prisma.org.findUnique({ + where: { + id: orgId, + }, + }); - if (!org || !org.stripeCustomerId) { - return notFound(); - } + if (!org || !org.stripeCustomerId) { + return notFound(); + } - const subscriptions = await stripe.subscriptions.list({ - customer: org.stripeCustomerId! - }) + const subscriptions = await stripe.subscriptions.list({ + customer: org.stripeCustomerId + }); - if (subscriptions.data.length === 0) { - return notFound(); - } - return subscriptions.data[0]; -} + if (subscriptions.data.length === 0) { + return notFound(); + } + return subscriptions.data[0]; + })); export const checkIfUserHasOrg = async (userId: string): Promise => { const orgs = await prisma.userToOrg.findMany({ @@ -610,7 +613,7 @@ export const removeMember = async (memberId: string, domain: string): Promise<{ } if (org.stripeCustomerId) { - const subscription = await fetchSubscription(orgId); + const subscription = await fetchSubscription(domain); if (isServiceError(subscription)) { return orgInvalidSubscription(); } @@ -645,7 +648,7 @@ export const removeMember = async (memberId: string, domain: string): Promise<{ export const getSubscriptionData = async (domain: string) => withAuth(async (session) => withOrgMembership(session, domain, async (orgId) => { - const subscription = await fetchSubscription(orgId); + const subscription = await fetchSubscription(domain); if (isServiceError(subscription)) { return orgInvalidSubscription(); } diff --git a/packages/web/src/app/[domain]/layout.tsx b/packages/web/src/app/[domain]/layout.tsx index 8e2a2858..7b665670 100644 --- a/packages/web/src/app/[domain]/layout.tsx +++ b/packages/web/src/app/[domain]/layout.tsx @@ -43,7 +43,7 @@ export default async function Layout({ return } - const subscription = await fetchSubscription(org.id); + const subscription = await fetchSubscription(domain); if (isServiceError(subscription) || (subscription.status !== "active" && subscription.status !== "trialing")) { return (
diff --git a/packages/web/src/app/redeem/page.tsx b/packages/web/src/app/redeem/page.tsx index ce8f27fc..d79a5747 100644 --- a/packages/web/src/app/redeem/page.tsx +++ b/packages/web/src/app/redeem/page.tsx @@ -6,8 +6,6 @@ import { AcceptInviteButton } from "./components/acceptInviteButton" import Image from "next/image"; import logoDark from "@/public/sb_logo_dark_large.png"; import logoLight from "@/public/sb_logo_light_large.png"; -import { fetchSubscription } from "@/actions"; -import { isServiceError } from "@/lib/utils"; interface RedeemPageProps { searchParams?: { @@ -110,36 +108,6 @@ export default async function RedeemPage({ searchParams }: RedeemPageProps) { ) } - const stripeCustomerId = org.stripeCustomerId; - if (stripeCustomerId) { - const subscription = await fetchSubscription(org.id); - console.log(org); - console.log(subscription); - if (isServiceError(subscription)) { - return ( -
-
- {"Sourcebot - {"Sourcebot -
-
-

This organization's subscription has expired. Please renew the subscription and try again.

-
-
- ) - } - } - return (