@base44-preview/sdk 0.8.24-pr.159.d5c24bd → 0.8.25-pr.134.d383574

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,3 +1,57 @@
1
+ function isInsideIframe() {
2
+ if (typeof window === "undefined")
3
+ return false;
4
+ return window !== window.parent;
5
+ }
6
+ /**
7
+ * Opens a URL in a centered popup and waits for the backend to postMessage
8
+ * the auth result back. On success, redirects the current window to
9
+ * redirectUrl with the token params appended, preserving the same behaviour
10
+ * as a normal full-page redirect flow.
11
+ *
12
+ * @param url - The login URL to open in the popup (should include popup_origin).
13
+ * @param redirectUrl - Where to redirect after auth (the original fromUrl).
14
+ * @param expectedOrigin - The origin we expect the postMessage to come from.
15
+ */
16
+ function loginViaPopup(url, redirectUrl, expectedOrigin) {
17
+ const width = 500;
18
+ const height = 600;
19
+ const left = Math.round(window.screenX + (window.outerWidth - width) / 2);
20
+ const top = Math.round(window.screenY + (window.outerHeight - height) / 2);
21
+ const popup = window.open(url, "base44_auth", `width=${width},height=${height},left=${left},top=${top},resizable=yes,scrollbars=yes`);
22
+ if (!popup) {
23
+ return;
24
+ }
25
+ const cleanup = () => {
26
+ window.removeEventListener("message", onMessage);
27
+ clearInterval(pollTimer);
28
+ if (!popup.closed)
29
+ popup.close();
30
+ };
31
+ const onMessage = (event) => {
32
+ var _a;
33
+ if (event.origin !== expectedOrigin)
34
+ return;
35
+ if (event.source !== popup)
36
+ return;
37
+ if (!((_a = event.data) === null || _a === void 0 ? void 0 : _a.access_token))
38
+ return;
39
+ cleanup();
40
+ const callbackUrl = new URL(redirectUrl);
41
+ const { access_token, is_new_user } = event.data;
42
+ callbackUrl.searchParams.set("access_token", access_token);
43
+ if (is_new_user != null) {
44
+ callbackUrl.searchParams.set("is_new_user", String(is_new_user));
45
+ }
46
+ window.location.href = callbackUrl.toString();
47
+ };
48
+ // Only used to detect the user closing the popup before auth completes
49
+ const pollTimer = setInterval(() => {
50
+ if (popup.closed)
51
+ cleanup();
52
+ }, 500);
53
+ window.addEventListener("message", onMessage);
54
+ }
1
55
  /**
2
56
  * Creates the auth module for the Base44 SDK.
3
57
  *
@@ -49,7 +103,13 @@ export function createAuthModule(axios, functionsAxiosClient, appId, options) {
49
103
  authPath = `/apps/auth${providerPath}/login`;
50
104
  }
51
105
  const loginUrl = `${options.appBaseUrl}/api${authPath}?${queryParams}`;
52
- // Redirect to the provider login page
106
+ // When running inside an iframe, use a popup to avoid OAuth providers
107
+ // blocking iframe navigation.
108
+ if (isInsideIframe()) {
109
+ const popupLoginUrl = `${loginUrl}&popup_origin=${encodeURIComponent(window.location.origin)}`;
110
+ return loginViaPopup(popupLoginUrl, redirectUrl, window.location.origin);
111
+ }
112
+ // Default: full-page redirect
53
113
  window.location.href = loginUrl;
54
114
  },
55
115
  // Logout the current user
@@ -33,6 +33,9 @@ export function createConnectorsModule(axios, appId) {
33
33
  connectionConfig: (_a = data.connection_config) !== null && _a !== void 0 ? _a : null,
34
34
  };
35
35
  },
36
+ /**
37
+ * @deprecated Use getCurrentAppUserConnection(connectorId) and use the returned accessToken (and connectionConfig when needed) instead.
38
+ */
36
39
  async getCurrentAppUserAccessToken(connectorId) {
37
40
  if (!connectorId || typeof connectorId !== "string") {
38
41
  throw new Error("Connector ID is required and must be a string");
@@ -41,6 +44,18 @@ export function createConnectorsModule(axios, appId) {
41
44
  const data = response;
42
45
  return data.access_token;
43
46
  },
47
+ async getCurrentAppUserConnection(connectorId) {
48
+ var _a;
49
+ if (!connectorId || typeof connectorId !== "string") {
50
+ throw new Error("Connector ID is required and must be a string");
51
+ }
52
+ const response = await axios.get(`/apps/${appId}/app-user-auth/connectors/${connectorId}/token`);
53
+ const data = response;
54
+ return {
55
+ accessToken: data.access_token,
56
+ connectionConfig: (_a = data.connection_config) !== null && _a !== void 0 ? _a : null,
57
+ };
58
+ },
44
59
  };
45
60
  }
46
61
  /**
@@ -32,6 +32,15 @@ export interface ConnectorConnectionResponse {
32
32
  /** Key-value configuration for the connection, or `null` if the connector does not provide one. */
33
33
  connectionConfig: Record<string, string> | null;
34
34
  }
35
+ /**
36
+ * Connection details for an app-user connector.
37
+ */
38
+ export interface AppUserConnectorConnectionResponse {
39
+ /** The OAuth access token for the end user's connection. */
40
+ accessToken: string;
41
+ /** Key-value configuration for the connection, or `null` if the connector does not provide one. */
42
+ connectionConfig: Record<string, string> | null;
43
+ }
35
44
  /**
36
45
  * Connectors module for managing app-scoped OAuth tokens for external services.
37
46
  *
@@ -226,6 +235,8 @@ export interface ConnectorsModule {
226
235
  /**
227
236
  * Retrieves an OAuth access token for an end user's connection to a specific connector.
228
237
  *
238
+ * @deprecated Use {@link getCurrentAppUserConnection} instead.
239
+ *
229
240
  * Returns the OAuth token string that belongs to the currently authenticated end user
230
241
  * for the specified connector.
231
242
  *
@@ -243,6 +254,27 @@ export interface ConnectorsModule {
243
254
  * ```
244
255
  */
245
256
  getCurrentAppUserAccessToken(connectorId: string): Promise<string>;
257
+ /**
258
+ * Retrieves the OAuth access token and connection configuration for an end user's
259
+ * connection to a specific connector.
260
+ *
261
+ * Returns both the OAuth token and any connection-specific configuration that
262
+ * belongs to the currently authenticated end user for the specified connector.
263
+ *
264
+ * @param connectorId - The connector ID (OrgConnector database ID).
265
+ * @returns Promise resolving to an {@link AppUserConnectorConnectionResponse} with `accessToken` and `connectionConfig`.
266
+ *
267
+ * @example
268
+ * ```typescript
269
+ * // Get the end user's connection details for a connector
270
+ * const { accessToken, connectionConfig } = await base44.asServiceRole.connectors.getCurrentAppUserConnection('abc123def');
271
+ *
272
+ * const response = await fetch('https://www.googleapis.com/calendar/v3/calendars/primary/events', {
273
+ * headers: { 'Authorization': `Bearer ${accessToken}` }
274
+ * });
275
+ * ```
276
+ */
277
+ getCurrentAppUserConnection(connectorId: string): Promise<AppUserConnectorConnectionResponse>;
246
278
  }
247
279
  /**
248
280
  * User-scoped connectors module for managing app-user OAuth connections.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44-preview/sdk",
3
- "version": "0.8.24-pr.159.d5c24bd",
3
+ "version": "0.8.25-pr.134.d383574",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",