@pylonsync/react 0.3.140 → 0.3.144

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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/src/useSession.ts +23 -6
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.3.140",
6
+ "version": "0.3.144",
7
7
  "type": "module",
8
8
  "main": "src/index.ts",
9
9
  "types": "src/index.ts",
@@ -12,8 +12,8 @@
12
12
  "check": "tsc -p tsconfig.json --noEmit"
13
13
  },
14
14
  "dependencies": {
15
- "@pylonsync/sdk": "0.3.140",
16
- "@pylonsync/sync": "0.3.140"
15
+ "@pylonsync/sdk": "0.3.144",
16
+ "@pylonsync/sync": "0.3.144"
17
17
  },
18
18
  "peerDependencies": {
19
19
  "react": ">=19.0.0"
package/src/useSession.ts CHANGED
@@ -14,9 +14,22 @@ export interface UseSessionReturn {
14
14
  isAdmin: boolean;
15
15
  isAuthenticated: boolean;
16
16
  /**
17
- * Force a refresh of the cached session. Call after sign-in, sign-out,
18
- * or `/api/auth/select-org` so the UI and sync engine pick up the
19
- * change before the next pull/reconnect.
17
+ * Switch the active tenant. POSTs to /api/auth/select-org, verifies
18
+ * membership server-side, resets the local replica so stale rows
19
+ * from the previous tenant disappear, and refreshes the cached
20
+ * session — all in one await. Throws (with `.code`/`.status`) on
21
+ * NOT_A_MEMBER and other server errors.
22
+ */
23
+ selectOrg: (orgId: string) => Promise<void>;
24
+ /** Drop the active tenant (back to no-org state). */
25
+ clearOrg: () => Promise<void>;
26
+ /** Revoke the current session server-side and refresh. */
27
+ signOut: () => Promise<void>;
28
+ /**
29
+ * Escape hatch — refresh the cached session without mutating it.
30
+ * Most apps shouldn't need this; the helpers above already include
31
+ * a refresh. Useful when app code calls /api/auth/* via its own
32
+ * client and needs the engine to observe the change.
20
33
  */
21
34
  refresh: () => Promise<void>;
22
35
  }
@@ -26,9 +39,10 @@ export interface UseSessionReturn {
26
39
  *
27
40
  * The engine fetches `/api/auth/me` on start and on token flips, caches
28
41
  * the result, and notifies the store on change — so this hook is
29
- * purely a reader. Mutations (login/logout/select-org) are still the
30
- * caller's responsibility; after them, invoke `refresh()` (or
31
- * `engine.notifySessionChanged()`) to pull the new session immediately.
42
+ * purely a reader. The exposed helpers (selectOrg, clearOrg, signOut)
43
+ * combine the server fetch + cache refresh + replica reset into one
44
+ * call so app code doesn't need to remember the manual
45
+ * `notifySessionChanged()` step after every auth mutation.
32
46
  */
33
47
  export function useSession(sync: SyncEngine): UseSessionReturn {
34
48
  const session = useSyncExternalStore(
@@ -68,6 +82,9 @@ export function useSession(sync: SyncEngine): UseSessionReturn {
68
82
  tenantId: session.tenantId,
69
83
  isAdmin: session.isAdmin,
70
84
  isAuthenticated: session.userId != null,
85
+ selectOrg: (orgId: string) => sync.selectOrg(orgId),
86
+ clearOrg: () => sync.clearOrg(),
87
+ signOut: () => sync.signOut(),
71
88
  refresh: () => sync.notifySessionChanged(),
72
89
  };
73
90
  }