> Put a feature behind a paywall: check access with your own code, open a checkout when it fails, then check again after the purchase.

Source: https://plandalf.com/docs/developers/gates

Developers

# Gates

A gate puts a feature in your app behind a checkout. When someone uses the feature, your access check runs. If it passes, the feature runs. If not, Plandalf opens the checkout, and after a purchase your check runs again with fresh data.

Plandalf doesn't know what your users have paid for: your app does. Gates run in the browser and call your own check and sync functions.

## Register a gate

`plandalf.addGate(name, checkout, check)` names the gate, the checkout slug to open, and a function that returns `true` when the user already has access. The check can be async.

```js
plandalf.addGate('export', 'upgrade-to-pro', () => user.plan !== 'free');
plandalf.addGate('seats', 'add-seats', () => team.seats < team.limit);
plandalf.addGate('course', 'buy-course'); // no check: always opens the checkout
```

Access means the check returned exactly `true`. Any other value, including a truthy one like `1` or a string, counts as no access.

## Use it

`plandalf.gate(name)` runs the gate and resolves with the outcome.

```js
document.querySelector('#export').addEventListener('click', async () => {
  const { access } = await plandalf.gate('export');
  if (access) exportReport();
});
```

## Refresh access after a purchase

After a purchase, a gate with a check runs that check again. Unless you refresh your data, the check still sees the old plan and access stays `false`. Pass a sync function as the second argument: it gets the purchase and returns fresh data, which the SDK hands to your check.

```js
plandalf.addGate('seats', 'add-seats', (fresh = team) => fresh.seats < fresh.limit);

const result = await plandalf.gate('seats', async (transaction) => {
  return await fetch('/api/team').then((r) => r.json()); // your server's current view
});

if (result.access) addMember();
else if (result.status === 'purchased') showPending(); // paid, but your server hasn't caught up yet
```

Whatever the sync function returns is also in `result.entitlements`. A gate with no check grants access as soon as the purchase completes.

The third argument takes the same options as `present()`, such as `{ frame: 'slideout' }`.

## Gate results

`status`

`access`

What happened

`existing`

`true`

The check passed; no checkout opened.

`purchased`

The check's new answer

The buyer completed the checkout. Without a check, `access` is `true`.

`dismissed`

`false`

The buyer closed the checkout.

`error`

`false`

The gate isn't registered, or the checkout, check or sync function failed. `result.error` has the cause.

After a purchase, `result.transaction` holds what the checkout returned, including the checkout slug and the customer.

**Check again on your server**

A gate decides what your page shows. It isn't proof of payment. Whatever the feature does on your server, check the user's access there too, from your own records or Plandalf's [webhooks](https://plandalf.com/docs/api/webhooks).

## In React

`<Gate>` shows its children when the user has access and a fallback when they don't. Inside the fallback, `useGateTrigger()` opens the checkout.

```tsx
import { Gate, useGateTrigger } from '@plandalf/react';

function UpgradePrompt() {
  const { trigger, checking } = useGateTrigger();
  return <button disabled={checking} onClick={() => trigger()}>Upgrade to export</button>;
}

export function ExportPanel({ user }) {
  return (
    <Gate name="export" flow="upgrade-to-pro" check={() => user.plan !== 'free'} fallback={<UpgradePrompt />}>
      <ExportButton />
    </Gate>
  );
}
```

`useGate(name, checkout, check)` gives you the same pieces as values: `access` (`null` while the check runs), `checking`, `result` and `trigger(sync, options)`. See [React](https://plandalf.com/docs/developers/react).

Something wrong or missing? Email [hello@plandalf.com](mailto:hello@plandalf.com).
