# Feature gate

> Content shown only when the active organization's plan includes a feature, read from the session's features claim and updated when the session refreshes.

- Block: [entitlements](https://bettersupabase.com/blocks/entitlements.md)
- Kind: component
- Hook: `useFeatureGate` from `@/hooks/use-entitlements`
- Page: https://bettersupabase.com/recipes/feature-gate

## Install the block

```bash
npx better-supabase add entitlements
```

## Web (Next.js)

Add the shadcn components:

```bash
npx shadcn add button skeleton
```

Add the glue from the better-supabase registry:

```bash
npx shadcn add @better-supabase/use-entitlements
```

From `apps/examples/nextjs/src/features/billing/components/feature-gate.tsx` (https://github.com/ScaleDockHQ/better-supabase/blob/main/apps/examples/nextjs/src/features/billing/components/feature-gate.tsx):

```tsx
"use client";

import type { ReactNode } from "react";

import { LockIcon } from "lucide-react";
import { useExtracted } from "next-intl";

import type { Entitlement } from "@/lib/claims";

import { buttonVariants } from "@/components/ui/button";
import { Skeleton } from "@/components/ui/skeleton";
import { useFeatureGate } from "@/hooks/use-entitlements";
import { Link } from "@/i18n/navigation";

/**
 * Renders `children` only when the active organization's plan includes
 * `feature`, read from the session's `features` claim; otherwise a link to the
 * plans. UX only: RLS and the route behind it check the feature too.
 */
export function FeatureGate({
  feature,
  children,
}: {
  feature: Entitlement;
  children: ReactNode;
}) {
  const t = useExtracted("billing");
  const { allowed } = useFeatureGate(feature);

  if (allowed === undefined) {
    return <Skeleton className="h-8 w-28" aria-busy="true" />;
  }

  if (allowed) {
    return children;
  }

  return (
    <Link
      href="/settings/billing"
      className={buttonVariants({ variant: "outline", size: "sm" })}
      data-testid={`upgrade-${feature}`}
    >
      <LockIcon />
      {t("Upgrade to unlock")}
    </Link>
  );
}
```

## Native (Expo)

The snippet uses `ActivityIndicator`, `Text`, `View` from `react-native`, so it needs no styling library.

Add the glue from the better-supabase registry:

```bash
npx shadcn add @better-supabase/use-entitlements
```

From `apps/examples/expo-powersync/src/components/feature-gate.tsx` (https://github.com/ScaleDockHQ/better-supabase/blob/main/apps/examples/expo-powersync/src/components/feature-gate.tsx):

```tsx
import type { ReactNode } from "react";

import { ActivityIndicator, Text, View } from "react-native";

import { useFeatureGate } from "../hooks/use-entitlements";

/**
 * Renders `children` only when the active organization's plan includes
 * `feature`, read from the session's `features` claim. UX only: RLS checks the
 * feature too.
 */
export function FeatureGate({
  feature,
  children,
}: {
  readonly feature: string;
  readonly children: ReactNode;
}) {
  const { allowed } = useFeatureGate(feature);

  if (allowed === undefined) {
    return <ActivityIndicator />;
  }

  if (allowed) {
    return children;
  }

  return (
    <View style={{ padding: 12, borderWidth: 1, borderRadius: 12 }}>
      <Text style={{ fontWeight: "600" }}>Not in your plan</Text>
      <Text style={{ color: "#737373" }}>
        Upgrade to a plan with {feature} to use this.
      </Text>
    </View>
  );
}
```

## Prompt

Paste one of these into a coding agent to build the recipe in your app.

### Web

```text
Build "Feature gate" in a React web app that uses shadcn/ui components. Content shown only when the active organization's plan includes a feature, read from the session's features claim and updated when the session refreshes.

It uses the better-supabase `entitlements` block (Stripe entitlements). It calls the hook `useFeatureGate` from `@/hooks/use-entitlements`.

Steps:

1. If the app doesn't have the block yet, add the SQL with `npx better-supabase add entitlements` and apply the migration.
2. Add the shadcn components the recipe uses: `npx shadcn add button skeleton`.
3. Add the glue from the better-supabase registry: `npx shadcn add @better-supabase/use-entitlements`. The items are copied into the app, so edit them as needed.
4. Write the component. Call `useFeatureGate` from `@/hooks/use-entitlements`, starting from the recipe's snippet (`apps/examples/nextjs/src/features/billing/components/feature-gate.tsx`). Adapt it to the app's existing components, styling and data loading instead of copying it as is.
5. Check the result against the recipe and the block's docs: it typechecks, it calls the same hook, and it handles the loading and empty states the snippet handles.

Read the full code first:

- Recipe: https://bettersupabase.com/recipes/feature-gate.md
- Block docs: https://bettersupabase.com/docs/blocks/entitlements
```

### Native

```text
Build "Feature gate" in a React Native app built with Expo. Content shown only when the active organization's plan includes a feature, read from the session's features claim and updated when the session refreshes.

It uses the better-supabase `entitlements` block (Stripe entitlements). It calls the hook `useFeatureGate` from `@/hooks/use-entitlements`.

Steps:

1. If the app doesn't have the block yet, add the SQL with `npx better-supabase add entitlements` and apply the migration.
2. The recipe uses `ActivityIndicator`, `Text`, `View` from `react-native`, so it needs no extra UI library. Use the app's own components if it has them.
3. Add the glue from the better-supabase registry: `npx shadcn add @better-supabase/use-entitlements`. The items are copied into the app, so edit them as needed.
4. Write the component. Call `useFeatureGate` from `@/hooks/use-entitlements`, starting from the recipe's snippet (`apps/examples/expo-powersync/src/components/feature-gate.tsx`). Adapt it to the app's existing components, styling and data loading instead of copying it as is.
5. Check the result against the recipe and the block's docs: it typechecks, it calls the same hook, and it handles the loading and empty states the snippet handles.

Read the full code first:

- Recipe: https://bettersupabase.com/recipes/feature-gate.md
- Block docs: https://bettersupabase.com/docs/blocks/entitlements
```
