@darkauth/client 1.20.1 → 1.20.3

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/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?: "silent" | "authorize" | "hosted";
57
+ mode?: "token" | "silent" | "authorize" | "hosted";
58
58
  returnTo?: string;
59
59
  };
60
60
  export type RefreshSessionOptions = {
package/dist/index.js CHANGED
@@ -686,7 +686,45 @@ export async function getSessionInfo() {
686
686
  };
687
687
  }
688
688
  export async function switchOrganization(organizationId, options = {}) {
689
- const mode = options.mode || "silent";
689
+ const mode = options.mode || "token";
690
+ if (mode === "token") {
691
+ const current = getStoredSession();
692
+ const bearerToken = current?.accessToken || current?.idToken;
693
+ if (!current || !bearerToken) {
694
+ await initiateLogin({ organizationId, returnTo: options.returnTo });
695
+ return null;
696
+ }
697
+ const response = await fetch(rootEndpoint("/api/token/organization"), {
698
+ method: "POST",
699
+ headers: {
700
+ "content-type": "application/json",
701
+ authorization: `Bearer ${bearerToken}`,
702
+ },
703
+ body: JSON.stringify({
704
+ organization_id: organizationId,
705
+ client_id: cfg.clientId,
706
+ }),
707
+ credentials: fetchCredentials(),
708
+ });
709
+ if (!response.ok)
710
+ throw await errorForResponse(response);
711
+ const tokenResponse = await response.json();
712
+ const idToken = tokenResponse.id_token;
713
+ const accessToken = typeof tokenResponse.access_token === "string"
714
+ ? tokenResponse.access_token
715
+ : undefined;
716
+ const refreshToken = refreshMode() === "token" ? tokenResponse.refresh_token : undefined;
717
+ return storeSession({
718
+ idToken,
719
+ accessToken,
720
+ drk: current.drk || EMPTY_DRK,
721
+ clientAppKey: current.clientAppKey,
722
+ rootKey: current.rootKey,
723
+ deliveredKeyKind: current.deliveredKeyKind,
724
+ keyDeliveryVersion: current.keyDeliveryVersion,
725
+ refreshToken,
726
+ });
727
+ }
690
728
  if (mode === "silent") {
691
729
  const response = await fetch(rootEndpoint("/api/user/session/organization"), {
692
730
  method: "POST",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkauth/client",
3
- "version": "1.20.1",
3
+ "version": "1.20.3",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "directory": "packages/darkauth-client",