Skip to content
← All posts

·

How to gate a feature behind a paid plan

Lock a feature to paying users without calling Stripe or Dodo on every request. The steps by hand, and what Launchfast's getBillingStatus() does.

Short answer: keep a copy of every payment and subscription in your own database, kept current by webhooks, and write one function that turns those rows into "which plan does this user have". Check it on the server, in the page and in every action. Launchfast already ships that function: getBillingStatus(userId) in src/lib/billing/index.ts reads only your own tables and returns the user's plan, so a gate is one line.

Why is gating a feature harder than an if statement?

The check itself is simple. Knowing the answer is the hard part:

  • The payment provider knows who paid, but calling its API on every page view is slow, rate limited and breaks your app when the provider has a bad hour.
  • A one-time purchase and a subscription grant access in different ways. A subscription can be active, past due, paused or cancelled but still paid until the end of the period.
  • A buyer returns from checkout before the webhook arrives, so for a few seconds they have paid and you don't know yet.
  • If the gate only exists in the UI, anyone can call the server action or API route directly.
  • Webhooks arrive late, twice, or out of order, so a stale event must not take access away from someone who just renewed.

How do you gate a feature by hand?

  1. Store payments and subscriptions. Create a payment table for successful charges and a subscription table with a status, the product or price ID and the current period end. Link both to your user through a customer row you create on the server at checkout, never from IDs sent by the browser.
  2. Fill them from webhooks. Verify the signature on the raw request, make the handler idempotent because providers retry, and ignore a subscription event that is older than the one you stored. The details are in how to accept payments in your SaaS.
  3. Map product IDs to plans. The provider gives you an ID such as a product or price ID. Keep a list in your code that says which ID is which plan, so a database row becomes a plan name.
  4. Write the access rule once. Decide what counts: for example a paid one-time purchase, or a subscription whose status is active. Put it in one function, so a pricing change touches one file.
  5. Query only your tables. The function takes a user ID and runs two small queries. It never calls the provider.
  6. Read the user on the server. Get the session in a server component or action, then call the function with that user's ID.
  7. Gate in every place that matters. Check in the page, in each server action and in each API route that does the paid work. Hiding a button is not a gate.
  8. Handle the gap after checkout. Show a "payment received, unlocking" state and poll a small status endpoint until the webhook lands.
  9. Wrap it for Next.js 16. With cacheComponents: true, reading the session must happen inside a component wrapped in <Suspense>.

The traps are steps 4 and 8. Teams that spread the access rule across files end up with a page that unlocks for a cancelled user and an API that doesn't, and nobody knows which is right.

What does Launchfast already do?

Launchfast has the tables, the webhooks and the rule. Here is what is in the repository.

The rule. resolveBillingStatus() in src/lib/billing/index.ts is the only place that decides access:

if (paid) return { plan: getPlanByProductId(paid.productId ?? "") ?? null, source: "purchase", subscription: sub };
if (latestSub?.status === "active") {
  return { plan: getPlanByProductId(latestSub.productId) ?? null, source: "subscription", subscription: sub };
}
return { plan: null, source: null, subscription: sub };

A paid one-time purchase wins, otherwise an active subscription counts. Anyone else gets plan: null. The result also carries the subscription's status, currentPeriodEnd and cancelAtPeriodEnd, so you can show "ends on this date" for someone who has cancelled.

The lookup. getBillingStatus(userId) runs two queries against the payment and subscription tables with Promise.all and passes the rows to that rule. It works the same whether the buyer paid through Dodo Payments or Stripe, because both webhook handlers write the same tables. getPlanByProductId() in src/config.ts accepts either provider's ID, so a purchase made before you switched provider still grants access.

Gating a feature. In a server component, read the user and the plan:

const user = await requireUser();
const { plan } = await getBillingStatus(user.id);
if (plan?.slug !== "launchfast") redirect("/#pricing");

Replace the slug with one of your own plans in src/config.ts. If any paid plan should unlock the feature, check plan alone. Put this inside a component wrapped in <Suspense>, as the Next.js 16 rules in AGENTS.md require.

Gating an action. Launchfast repeats the check on the server where the paid work happens. In src/lib/repo-access-actions.ts, the action that invites a buyer to the private repository does this:

const user = await requireUser();
// Checked here, not just in the UI: only paying users get the code.
if (!(await getBillingStatus(user.id)).plan) return { error: "Repository access comes with a paid plan." };

The gap after checkout. src/app/api/billing/status/route.ts returns the user's plan slug with Cache-Control: no-store and a 401 when signed out. The page buyers return to polls it until the webhook has written the payment.

Keeping the data right. The webhook handlers in src/lib/billing/dodo.ts and src/lib/billing/stripe.ts verify the signature and are safe to run twice. The Dodo handler ignores subscription events older than the stored providerUpdatedAt, and the Stripe handler reads the subscription from the Stripe API instead of trusting the event, because Stripe doesn't guarantee event order. Either way, a late event can't remove access from someone who just renewed.

What should you know before relying on it?

Access is only as fresh as your webhooks. If a webhook never arrives, the tables stay as they were, so check delivery in your provider's dashboard. A subscription that is on_hold or paused does not count as active, so it gets no plan. The kit has one plan by default; add more in plans and gate each by its slug. The gate also controls what the page shows, not what the database holds, so keep paid data behind the same server-side check.

How much does this save?

You skip the access rule, the product ID mapping, the two queries and the polling endpoint. What is left is one line in each page or action you want to lock.

Launchfast is the starter this post describes. Related reading: how to accept payments in your SaaS, how to build a SaaS admin dashboard with MRR and what happens when Dodo Payments sends the same webhook twice.