Skip to content
This repository was archived by the owner on Oct 1, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions app/src/lib/auth/billing.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export const APP_BILLING_PLAN_SLUGS = [
const DEFAULT_APP_BILLING_PLAN_SLUGS = [
'early_access',
'early-access',
'starter',
'homesteader',
'producer',
'professional',
'commercial',
Expand All @@ -12,6 +14,28 @@ export const AGENT_MONITOR_FEATURE_SLUGS = [
'agent_dashboard', // Legacy slug kept for backward compatibility
] as const

const DEFAULT_APP_BILLING_FEATURE_SLUGS = AGENT_MONITOR_FEATURE_SLUGS

function parseSlugList(raw: string | undefined): string[] {
if (!raw) return []
return raw
.split(',')
.map((s) => s.trim())
.filter((s) => s.length > 0)
}

export function getAppBillingPlanSlugs(): string[] {
const fromEnv = parseSlugList(import.meta.env.VITE_BILLING_PLAN_SLUGS as string | undefined)
if (fromEnv.length === 0) return [...DEFAULT_APP_BILLING_PLAN_SLUGS]
return Array.from(new Set([...DEFAULT_APP_BILLING_PLAN_SLUGS, ...fromEnv]))
}

export function getAppBillingFeatureSlugs(): string[] {
const fromEnv = parseSlugList(import.meta.env.VITE_BILLING_FEATURE_SLUGS as string | undefined)
if (fromEnv.length === 0) return [...DEFAULT_APP_BILLING_FEATURE_SLUGS]
return Array.from(new Set([...DEFAULT_APP_BILLING_FEATURE_SLUGS, ...fromEnv]))
}

function safeCheck(check: (value: string) => boolean, value: string): boolean {
try {
return check(value)
Expand All @@ -22,7 +46,7 @@ function safeCheck(check: (value: string) => boolean, value: string): boolean {

export function hasAnyPlan(
hasPlan: (plan: string) => boolean,
plans: readonly string[] = APP_BILLING_PLAN_SLUGS
plans: readonly string[] = getAppBillingPlanSlugs()
): boolean {
return plans.some((plan) => safeCheck(hasPlan, plan))
}
Expand All @@ -33,3 +57,14 @@ export function hasAnyFeature(
): boolean {
return features.some((feature) => safeCheck(hasFeature, feature))
}

export function hasBillingAccess(args: {
hasPlan: (plan: string) => boolean
hasFeature: (feature: string) => boolean
planSlugs?: readonly string[]
featureSlugs?: readonly string[]
}): boolean {
const planSlugs = args.planSlugs ?? getAppBillingPlanSlugs()
const featureSlugs = args.featureSlugs ?? getAppBillingFeatureSlugs()
return hasAnyPlan(args.hasPlan, planSlugs) || hasAnyFeature(args.hasFeature, featureSlugs)
}
15 changes: 12 additions & 3 deletions app/src/routes/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@ import { useEffect } from 'react'
import { TutorialProvider, TutorialOverlay } from '@/components/onboarding/tutorial'
import { SatelliteAnimationProvider } from '@/lib/satellite-animation'
import { SatelliteCollapseAnimation } from '@/components/layout/SatelliteCollapseAnimation'
import { APP_BILLING_PLAN_SLUGS, hasAnyPlan } from '@/lib/auth/billing'
import { hasBillingAccess } from '@/lib/auth/billing'

export const Route = createFileRoute('/app')({
component: AppLayout,
})

function AppLayout() {
const { isLoaded, isSignedIn, isDevAuth, needsOnboarding, isOrgLoaded, hasPlan: checkPlan, isPlanLoaded } = useAppAuth()
const {
isLoaded,
isSignedIn,
isDevAuth,
needsOnboarding,
isOrgLoaded,
hasPlan: checkPlan,
hasFeature: checkFeature,
isPlanLoaded,
} = useAppAuth()
const navigate = useNavigate()
const location = useRouterState({ select: (s) => s.location })
const isOnboarding = location.pathname === '/app/onboarding'
Expand All @@ -30,7 +39,7 @@ function AppLayout() {
const isSubscriptionLoaded = isPlanLoaded
const hasPlan = paywallEnabled
? (isSubscriptionLoaded
? hasAnyPlan(checkPlan, APP_BILLING_PLAN_SLUGS)
? hasBillingAccess({ hasPlan: checkPlan, hasFeature: checkFeature })
: false)
: true

Expand Down
26 changes: 17 additions & 9 deletions app/src/routes/subscribe.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { createFileRoute, useNavigate, useSearch } from '@tanstack/react-router'
import { useUser, useClerk, useAuth, PricingTable } from '@clerk/clerk-react'
import { useEffect, useCallback } from 'react'
import { useEffect, useCallback, useMemo } from 'react'
import { Button } from '@/components/ui/button'
import { Terminal, LogOut } from 'lucide-react'
import { LoadingSpinner } from '@/components/ui/loading/LoadingSpinner'
import { useAppAuth } from '@/lib/auth'
import { useAnalytics } from '@/lib/analytics'
import { APP_BILLING_PLAN_SLUGS, hasAnyPlan } from '@/lib/auth/billing'
import { getAppBillingFeatureSlugs, getAppBillingPlanSlugs, hasBillingAccess } from '@/lib/auth/billing'
import { z } from 'zod'

export const Route = createFileRoute('/subscribe')({
Expand Down Expand Up @@ -47,26 +47,34 @@ function SubscribePageContent() {
const { signOut } = useClerk()
const navigate = useNavigate()
const { trackSubscriptionStarted } = useAnalytics()
const billingPlanSlugs = useMemo(() => getAppBillingPlanSlugs(), [])
const billingFeatureSlugs = useMemo(() => getAppBillingFeatureSlugs(), [])

// B2C billing source of truth: Clerk plans/features via has().
const checkHasPlan = useCallback(() => {
if (!has) return false
return hasAnyPlan(
(plan) => has({ plan }),
APP_BILLING_PLAN_SLUGS
)
}, [has])
return hasBillingAccess({
hasPlan: (plan) => has({ plan }),
hasFeature: (feature) => has({ feature }),
planSlugs: billingPlanSlugs,
featureSlugs: billingFeatureSlugs,
})
}, [has, billingPlanSlugs, billingFeatureSlugs])

// Debug: log active plan checks
useEffect(() => {
if (isUserLoaded && user) {
console.log('[Subscribe] User ID:', user.id)
console.log(
'[Subscribe] Clerk plan checks:',
APP_BILLING_PLAN_SLUGS.map((plan) => [plan, has?.({ plan })])
billingPlanSlugs.map((plan) => [plan, has?.({ plan })])
)
console.log(
'[Subscribe] Clerk feature checks:',
billingFeatureSlugs.map((feature) => [feature, has?.({ feature })])
)
}
}, [isUserLoaded, user, has])
}, [isUserLoaded, user, has, billingPlanSlugs, billingFeatureSlugs])

// Paywall is enabled by default, can be disabled via env var (useful for dev)
const paywallDisabled = import.meta.env.VITE_PAYWALL_DISABLED === 'true'
Expand Down