Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

fix(billing): block non-admin members from payment/onboarding when subscription lapses (#317)

Gökhan Geyika626c9f7dec3
diff --git a/web/src/app/[locale]/(dashboard)/layout.tsx b/web/src/app/[locale]/(dashboard)/layout.tsx
index f786dca98..4fe105d77 100644
--- a/web/src/app/[locale]/(dashboard)/layout.tsx
+++ b/web/src/app/[locale]/(dashboard)/layout.tsx
@@ -10,6 +10,8 @@ import { BrandLoader } from '@/components/providers/brand-loader';
import { BrandGuard } from '@/components/providers/brand-guard';
import { getBrands } from '@/lib/actions/brand';
import { isCloud, type PlanId } from '@/config/plans';
+import { evaluateSubscriptionAccess } from '@/lib/billing/subscription-access';
+import { SubscriptionExpiredNotice } from '@/components/billing/subscription-expired-notice';
export default async function DashboardLayout({ children }: { children: React.ReactNode }) {
const supabase = await createClient();
@@ -42,12 +44,24 @@ export default async function DashboardLayout({ children }: { children: React.Re
getBrands(profile.organization_id),
]);
- // Cloud mode: require active or trialing subscription
+ // Cloud mode: gate access when the subscription isn't active/trialing.
+ // Admins (incl. the owner) are routed to the renew/payment flow; everyone
+ // else is blocked with a contact-your-owner notice — so an invited member
+ // can neither reach the payment screen nor walk back into onboarding to
+ // re-create the brand / competitors.
if (isCloud()) {
- const subStatus = org?.subscription_status as string | null;
- if (subStatus !== 'active' && subStatus !== 'trialing') {
+ const access = await evaluateSubscriptionAccess(
+ supabase,
+ profile.organization_id,
+ role,
+ (org?.subscription_status as string | null) ?? null,
+ );
+ if (access.state === 'needs_payment') {
redirect('/dashboard/onboarding');
}
+ if (access.state === 'blocked') {
+ return <SubscriptionExpiredNotice ownerName={access.ownerName} />;
+ }
}
const planId = (org?.plan ?? 'starter') as PlanId;
diff --git a/web/src/app/[locale]/(onboarding)/layout.tsx b/web/src/app/[locale]/(onboarding)/layout.tsx
index 60ca38d62..2d69ea539 100644
--- a/web/src/app/[locale]/(onboarding)/layout.tsx
+++ b/web/src/app/[locale]/(onboarding)/layout.tsx
@@ -2,6 +2,10 @@ import { redirect } from 'next/navigation';
import { createClient } from '@/lib/supabase/server';
import { AuthProvider } from '@/components/providers/auth-provider';
import { OnboardingSignOutButton } from '@/components/auth/onboarding-sign-out-button';
+import { isCloud } from '@/config/plans';
+import { evaluateSubscriptionAccess } from '@/lib/billing/subscription-access';
+import { SubscriptionExpiredNotice } from '@/components/billing/subscription-expired-notice';
+import type { TeamRole } from '@/components/providers/role-provider';
export default async function OnboardingLayout({ children }: { children: React.ReactNode }) {
const supabase = await createClient();
@@ -13,6 +17,35 @@ export default async function OnboardingLayout({ children }: { children: React.R
redirect('/sign-in');
}
+ // An already-onboarded non-admin whose org subscription has lapsed must not
+ // re-enter the wizard (they'd otherwise see the payment step and could
+ // re-create the brand / competitors on the org's existing setup). Admins keep
+ // the renew flow; not-yet-onboarded users (new org creators) pass through.
+ if (isCloud()) {
+ const { data: profile } = await supabase
+ .from('profiles')
+ .select('organization_id, onboarding_completed, role')
+ .eq('id', user.id)
+ .single();
+
+ if (profile?.onboarding_completed && profile.organization_id) {
+ const { data: org } = await supabase
+ .from('organizations')
+ .select('subscription_status')
+ .eq('id', profile.organization_id)
+ .single();
+ const access = await evaluateSubscriptionAccess(
+ supabase,
+ profile.organization_id,
+ (profile.role ?? 'analyst') as TeamRole,
+ (org?.subscription_status as string | null) ?? null,
+ );
+ if (access.state === 'blocked') {
+ return <SubscriptionExpiredNotice ownerName={access.ownerName} />;
+ }
+ }
+ }
+
return (
<>
<AuthProvider user={user} />
diff --git a/web/src/components/billing/subscription-expired-notice.tsx b/web/src/components/billing/subscription-expired-notice.tsx
new file mode 100644
index 000000000..77cc291e3
--- /dev/null
+++ b/web/src/components/billing/subscription-expired-notice.tsx
@@ -0,0 +1,31 @@
+import { AlertCircle } from 'lucide-react';
+import { OnboardingSignOutButton } from '@/components/auth/onboarding-sign-out-button';
+
+/**
+ * Shown to a non-admin member when their org's subscription has lapsed.
+ * They can't renew or re-onboard, so instead of the payment / onboarding flow
+ * they get a clear "contact your account owner" message (plus a way out).
+ */
+export function SubscriptionExpiredNotice({ ownerName }: { ownerName?: string | null }) {
+ return (
+ <div className="flex min-h-screen flex-col items-center justify-center bg-background p-6">
+ <div className="w-full max-w-md rounded-xl border bg-card p-8 text-center shadow-sm">
+ <div className="mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-amber-500/10">
+ <AlertCircle className="h-6 w-6 text-amber-600 dark:text-amber-400" />
+ </div>
+ <h1 className="mt-4 text-lg font-semibold">Subscription required</h1>
+ <p className="mt-2 text-sm text-muted-foreground">
+ This account&rsquo;s subscription has ended, so tracking and dashboard access are paused.
+ {ownerName
+ ? ` Please contact your account owner (${ownerName}) to renew it.`
+ : ' Please contact your account owner or an admin to renew it.'}
+ </p>
+ <p className="mt-3 text-xs text-muted-foreground">
+ You don&rsquo;t have billing access on this account, so you can&rsquo;t renew it or make
+ changes until it&rsquo;s active again.
+ </p>
+ </div>
+ <OnboardingSignOutButton />
+ </div>
+ );
+}
diff --git a/web/src/lib/billing/subscription-access.test.ts b/web/src/lib/billing/subscription-access.test.ts
new file mode 100644
index 000000000..e5dae285f
--- /dev/null
+++ b/web/src/lib/billing/subscription-access.test.ts
@@ -0,0 +1,58 @@
+import { describe, expect, it, vi } from 'vitest';
+
+import { evaluateSubscriptionAccess } from './subscription-access';
+
+// Minimal Supabase stub: the chained query builder resolves to `ownerRow` at
+// `.maybeSingle()`. Only the blocked (non-admin, inactive) path touches it.
+function fakeSupabase(ownerRow: { full_name: string | null } | null) {
+ const builder = {
+ select: () => builder,
+ eq: () => builder,
+ order: () => builder,
+ limit: () => builder,
+ maybeSingle: async () => ({ data: ownerRow, error: null }),
+ };
+ const from = vi.fn(() => builder);
+ // Cast through unknown — the helper only uses .from(...).select()....maybeSingle().
+ return { client: { from } as unknown as Parameters<typeof evaluateSubscriptionAccess>[0], from };
+}
+
+describe('evaluateSubscriptionAccess', () => {
+ it('returns ok for active/trialing regardless of role (no owner lookup)', async () => {
+ const { client, from } = fakeSupabase(null);
+ expect(await evaluateSubscriptionAccess(client, 'org', 'analyst', 'active')).toEqual({
+ state: 'ok',
+ });
+ expect(await evaluateSubscriptionAccess(client, 'org', 'admin', 'trialing')).toEqual({
+ state: 'ok',
+ });
+ expect(from).not.toHaveBeenCalled();
+ });
+
+ it('routes admins to the payment flow when the subscription is inactive', async () => {
+ const { client, from } = fakeSupabase(null);
+ expect(await evaluateSubscriptionAccess(client, 'org', 'admin', 'past_due')).toEqual({
+ state: 'needs_payment',
+ });
+ expect(await evaluateSubscriptionAccess(client, 'org', 'admin', null)).toEqual({
+ state: 'needs_payment',
+ });
+ expect(from).not.toHaveBeenCalled();
+ });
+
+ it('blocks non-admins when inactive and surfaces the owner name', async () => {
+ const { client } = fakeSupabase({ full_name: 'Jane Owner' });
+ expect(await evaluateSubscriptionAccess(client, 'org', 'manager', 'canceled')).toEqual({
+ state: 'blocked',
+ ownerName: 'Jane Owner',
+ });
+ });
+
+ it('falls back to a null owner name when the lookup returns nothing', async () => {
+ const { client } = fakeSupabase(null);
+ expect(await evaluateSubscriptionAccess(client, 'org', 'analyst', null)).toEqual({
+ state: 'blocked',
+ ownerName: null,
+ });
+ });
+});
diff --git a/web/src/lib/billing/subscription-access.ts b/web/src/lib/billing/subscription-access.ts
new file mode 100644
index 000000000..0f83a280b
--- /dev/null
+++ b/web/src/lib/billing/subscription-access.ts
@@ -0,0 +1,57 @@
+import type { TeamRole } from '@/components/providers/role-provider';
+
+// Type-only reference to the server client — no runtime import, so this helper
+// stays free of server-module coupling.
+type ServerSupabase = Awaited<ReturnType<typeof import('@/lib/supabase/server').createClient>>;
+
+export type SubscriptionAccess =
+ | { state: 'ok' }
+ | { state: 'needs_payment' }
+ | { state: 'blocked'; ownerName: string | null };
+
+const ACTIVE_STATUSES = new Set(['active', 'trialing']);
+
+/**
+ * Decide what an authenticated, onboarded org member may do when their org's
+ * subscription is not active.
+ *
+ * - active / trialing → 'ok' (full access)
+ * - inactive + admin → 'needs_payment' (admins, incl. the owner, can renew)
+ * - inactive + non-admin → 'blocked' (manager / analyst / agency_partner
+ * can't pay or re-onboard — they get a
+ * "contact your account owner" notice)
+ *
+ * This is the fix for the trial-ended invited-member case: previously *every*
+ * member was redirected into the onboarding wizard when the subscription
+ * lapsed, so a non-owner saw the payment screen and could walk back to step 1
+ * and re-create the brand / competitors on an already-set-up org.
+ *
+ * "Owner" is the org's first admin; we surface their name in the blocked
+ * message so a member knows who to contact. The lookup uses the caller's RLS
+ * client, so if it can't read the owner's row the message simply falls back to
+ * a generic wording (never throws).
+ */
+export async function evaluateSubscriptionAccess(
+ supabase: ServerSupabase,
+ organizationId: string,
+ role: TeamRole,
+ subscriptionStatus: string | null,
+): Promise<SubscriptionAccess> {
+ if (subscriptionStatus && ACTIVE_STATUSES.has(subscriptionStatus)) {
+ return { state: 'ok' };
+ }
+ if (role === 'admin') {
+ return { state: 'needs_payment' };
+ }
+
+ const { data: owner } = await supabase
+ .from('profiles')
+ .select('full_name')
+ .eq('organization_id', organizationId)
+ .eq('role', 'admin')
+ .order('created_at', { ascending: true })
+ .limit(1)
+ .maybeSingle();
+
+ return { state: 'blocked', ownerName: (owner?.full_name as string | null) ?? null };
+}