@nebulr-group/bridge-svelte 0.4.0-beta.5 → 0.4.0-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,15 @@
1
1
  <script lang="ts">
2
2
  import { beforeNavigate, goto } from '$app/navigation';
3
+ import { page } from '$app/stores';
3
4
  import { onMount, onDestroy } from 'svelte';
4
5
  import { createRouteGuard } from '../auth/route-guard.js';
5
- import { getBridgeAuth } from '../core/bridge-instance.js';
6
+ import {
7
+ getBridgeAuth,
8
+ isAuthenticated,
9
+ subscriptionStore,
10
+ loadSubscription,
11
+ ensureAppConfig,
12
+ } from '../core/bridge-instance.js';
6
13
  import { bridge as bridgeSurface } from '../core/bridge.js';
7
14
  import { setBridgeContext } from '../core/use-bridge.js';
8
15
  import { getConfig } from './stores/config.store.js';
@@ -11,7 +18,6 @@
11
18
  stopBridgeRuntime,
12
19
  type StartBridgeRuntimeOptions,
13
20
  } from '../core/bridge-runtime.js';
14
- import { loadSubscription, ensureAppConfig } from '../core/bridge-instance.js';
15
21
 
16
22
  // Props: optional `runtime` overrides for advanced/debug use (websocketFactory,
17
23
  // reconnect overrides, etc.); `onBootstrapComplete` callback fires after the
@@ -30,6 +36,42 @@
30
36
 
31
37
  const guard = createRouteGuard();
32
38
 
39
+ // Reactive paywall enforcement.
40
+ //
41
+ // The one-shot redirect in bridgeBootstrap() (load) only fires on the very
42
+ // first load — it short-circuits on every later navigation (bridgeReadyStore),
43
+ // so it misses the post-login case where auth + subscription state resolve a
44
+ // beat AFTER load() ran. This guard owns correctness: it actively loads the
45
+ // subscription once known-authenticated, then redirects reactively whenever
46
+ // `shouldSelectPlan` resolves true. Same data source as <BridgePaywall>, so the
47
+ // overlay and the redirect agree. The load() redirect remains a no-flash
48
+ // fast-path for direct loads/refreshes only.
49
+ $effect(() => {
50
+ const paywallRoute = getConfig().billing?.paywallRoute;
51
+ if (!paywallRoute || !$isAuthenticated) return;
52
+
53
+ const { status, loading, error } = $subscriptionStore;
54
+
55
+ // Status unknown → trigger a single load. Guarding on `!error` avoids a
56
+ // tight refetch loop on persistent failure (fail-pending, not fail-open);
57
+ // a workspace switch resets the store and re-attempts.
58
+ if (!status && !loading && !error) {
59
+ loadSubscription().catch(() => { /* surfaced via store.error */ });
60
+ return;
61
+ }
62
+
63
+ // Status known → enforce. `$page.url.pathname` makes this re-run on
64
+ // navigation too, so manual nav to a protected page while plan-less is
65
+ // also caught. Path guard prevents a redirect loop on the paywall itself.
66
+ if (
67
+ status?.shouldSelectPlan === true &&
68
+ status?.paymentsAutoRedirect !== false &&
69
+ $page.url.pathname !== paywallRoute
70
+ ) {
71
+ goto(paywallRoute);
72
+ }
73
+ });
74
+
33
75
  async function handleRoute(pathname: string, cancel?: () => void) {
34
76
  const decision = await guard.getNavigationDecision(pathname);
35
77
  if (decision.type === 'login') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nebulr-group/bridge-svelte",
3
- "version": "0.4.0-beta.5",
3
+ "version": "0.4.0-beta.6",
4
4
  "description": "Bridge Svelte library, This library helps you to add bridge authentication and feature flags, and payments to your svelte application.",
5
5
  "author": "Iman Pouya",
6
6
  "license": "MIT",