@darkauth/client 1.19.1 → 1.20.1

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.
package/README.md CHANGED
@@ -118,7 +118,7 @@ If `tokenStorage: 'localStorage'` or `drkStorage: 'localStorage'` is configured
118
118
 
119
119
  Refreshes the current session. In default first-party mode, the browser sends the DarkAuth refresh cookie and no JavaScript-readable refresh token is required. For non-ZK sessions, returns `drk` as an empty `Uint8Array`.
120
120
 
121
- Use `{ force: true }` after a hosted organization switch so the app receives tokens for the newly selected DarkAuth session organization even if the current in-memory ID token has not expired.
121
+ Use `{ force: true }` after changing the DarkAuth session organization so the app receives tokens for the newly selected organization even if the current in-memory ID token has not expired.
122
122
 
123
123
  ### Organization Switching
124
124
 
@@ -132,9 +132,9 @@ Returns the current user's organizations for app-owned switcher UI. Use `status`
132
132
 
133
133
  Returns current first-party session and organization context for app chrome before a fresh OAuth callback is needed.
134
134
 
135
- #### `switchOrganization(organizationId: string, options?: SwitchOrganizationOptions): Promise<void>`
135
+ #### `switchOrganization(organizationId: string, options?: SwitchOrganizationOptions): Promise<AuthSession | null>`
136
136
 
137
- Switches the selected organization. The default `authorize` mode starts a new authorization-code flow. `hosted` mode redirects to DarkAuth's `/switch-org` page.
137
+ Switches the selected organization. The default `silent` mode updates the DarkAuth session organization, forces a token refresh, and returns the refreshed session. `authorize` mode starts a new authorization-code flow. `hosted` mode redirects to DarkAuth's `/switch-org` page.
138
138
 
139
139
  #### App-owned switcher
140
140
 
@@ -143,7 +143,6 @@ Use this pattern when the app owns the workspace rail, menu, or account switcher
143
143
  ```typescript
144
144
  import {
145
145
  getCurrentUser,
146
- handleCallback,
147
146
  listOrganizations,
148
147
  switchOrganization,
149
148
  } from '@DarkAuth/client';
@@ -152,17 +151,14 @@ const organizations = await listOrganizations();
152
151
  const activeOrganizationId = getCurrentUser()?.org_id;
153
152
 
154
153
  async function selectOrganization(organizationId: string) {
155
- await switchOrganization(organizationId, {
156
- mode: 'authorize',
157
- returnTo: window.location.href,
158
- });
154
+ const session = await switchOrganization(organizationId);
155
+ const selectedOrganizationId = getCurrentUser()?.org_id;
159
156
  }
160
-
161
- const session = await handleCallback();
162
- const selectedOrganizationId = getCurrentUser()?.org_id;
163
157
  ```
164
158
 
165
- After the callback, verify that `selectedOrganizationId` matches the workspace being loaded. Treat the switch as a tenant or workspace state reset: clear tenant-local caches, selected resources, open realtime subscriptions, in-flight requests, and authorization decisions before loading data for the new `org_id`.
159
+ After the refresh, verify that `selectedOrganizationId` matches the workspace being loaded. Treat the switch as a tenant or workspace state reset: clear tenant-local caches, selected resources, open realtime subscriptions, in-flight requests, and authorization decisions before loading data for the new `org_id`.
160
+
161
+ Use `mode: 'authorize'` when a deployment should re-enter the redirect-based OAuth flow for every organization switch.
166
162
 
167
163
  #### Hosted switcher
168
164
 
package/dist/index.d.ts CHANGED
@@ -54,7 +54,7 @@ export type InitiateLoginOptions = {
54
54
  returnTo?: string;
55
55
  };
56
56
  export type SwitchOrganizationOptions = {
57
- mode?: "authorize" | "hosted";
57
+ mode?: "silent" | "authorize" | "hosted";
58
58
  returnTo?: string;
59
59
  };
60
60
  export type RefreshSessionOptions = {
@@ -92,6 +92,6 @@ export declare function getStoredSession(): AuthSession | null;
92
92
  export declare function refreshSession(options?: RefreshSessionOptions): Promise<AuthSession | null>;
93
93
  export declare function listOrganizations(): Promise<DarkAuthOrganization[]>;
94
94
  export declare function getSessionInfo(): Promise<DarkAuthSessionInfo>;
95
- export declare function switchOrganization(organizationId: string, options?: SwitchOrganizationOptions): Promise<void>;
95
+ export declare function switchOrganization(organizationId: string, options?: SwitchOrganizationOptions): Promise<AuthSession | null>;
96
96
  export declare function logout(): void;
97
97
  export declare function getCurrentUser(): JwtClaims | null;
package/dist/index.js CHANGED
@@ -686,9 +686,26 @@ export async function getSessionInfo() {
686
686
  };
687
687
  }
688
688
  export async function switchOrganization(organizationId, options = {}) {
689
- if ((options.mode || "authorize") === "authorize") {
689
+ const mode = options.mode || "silent";
690
+ if (mode === "silent") {
691
+ const response = await fetch(rootEndpoint("/api/user/session/organization"), {
692
+ method: "POST",
693
+ headers: { "content-type": "application/json" },
694
+ body: JSON.stringify({
695
+ organization_id: organizationId,
696
+ return_to: options.returnTo,
697
+ client_id: cfg.clientId,
698
+ }),
699
+ credentials: fetchCredentials(),
700
+ });
701
+ if (!response.ok)
702
+ throw await errorForResponse(response);
703
+ await response.json().catch(() => null);
704
+ return await refreshSession({ force: true });
705
+ }
706
+ if (mode === "authorize") {
690
707
  await initiateLogin({ organizationId, returnTo: options.returnTo });
691
- return;
708
+ return null;
692
709
  }
693
710
  const switchUrl = new URL(rootEndpoint("/switch-org"));
694
711
  switchUrl.searchParams.set("organization_id", organizationId);
@@ -697,6 +714,7 @@ export async function switchOrganization(organizationId, options = {}) {
697
714
  switchUrl.searchParams.set("return_to", options.returnTo);
698
715
  }
699
716
  location.assign(switchUrl.toString());
717
+ return null;
700
718
  }
701
719
  export function logout() {
702
720
  memorySession = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkauth/client",
3
- "version": "1.19.1",
3
+ "version": "1.20.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "directory": "packages/darkauth-client",