Protecting Content

Use the SDK’s user state and events to decide what to show: isAuthenticated, productAccess, and especially userChange, which is the main signal when auth or entitlements change.

Reading access

tiun.getUser() (and payloads on userChange) expose whether the user is signed in and which products they can use. Combine isAuthenticated with productAccess to gate pages, components, or API calls from the client.

Reacting to changes

Subscribe with tiun.on('userChange', …). This fires when the session initializes, after login, after checkout, and after logout — so you can keep navigation, paywalls, and premium areas in sync without polling.

When to use what

Scenario
Method
What happens

User subscribes

tiun.checkout({ productId })

Checkout includes auth; on success the user is authenticated and their access reflects the product.

Returning user

Automatic

Session is restored; userChange fires with event: 'init'.

Not authenticated

tiun.login()

Login overlay: email entry, then phone OTP verification; session is created when verification succeeds.

Sign out

tiun.logout()

Session cleared; userChange fires with event: 'logout'.

Example

tiun.on('userChange', (data) => {
  const { event, isAuthenticated, user } = data;
  if (!isAuthenticated || !user) {
    showPublicExperience();
    return;
  }
  const hasPremium = user.productAccess?.some(
    (p) => p.productId === 'YOUR_PRODUCT_ID'
  );
  if (hasPremium) {
    showPremiumContent();
  } else {
    showUpgradePrompt();
  }
});

For a deeper breakdown of fields and access rules, see Customer state: access.

Last updated

Was this helpful?