@base44-preview/sdk 0.8.19-pr.134.6049a1e → 0.8.19-pr.134.76a0e8e

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.
@@ -5,14 +5,16 @@ function isPopupAuthDomain() {
5
5
  return POPUP_AUTH_DOMAIN_REGEX.test(window.location.hostname);
6
6
  }
7
7
  /**
8
- * Opens a URL in a centered popup and, once the OAuth provider redirects
9
- * back to our origin, mirrors that callback URL in the current window so the
10
- * iframe processes the access_token query param exactly as a normal redirect
11
- * would.
8
+ * Opens a URL in a centered popup and waits for the backend to postMessage
9
+ * the auth result back. On success, redirects the current window to
10
+ * redirectUrl with the token params appended, preserving the same behaviour
11
+ * as a normal full-page redirect flow.
12
12
  *
13
- * @param url - The URL to open in the popup.
13
+ * @param url - The login URL to open in the popup (should include popup_origin).
14
+ * @param redirectUrl - Where to redirect after auth (the original fromUrl).
15
+ * @param expectedOrigin - The origin we expect the postMessage to come from.
14
16
  */
15
- function loginViaPopup(url) {
17
+ function loginViaPopup(url, redirectUrl, expectedOrigin) {
16
18
  const width = 500;
17
19
  const height = 600;
18
20
  const left = Math.round(window.screenX + (window.outerWidth - width) / 2);
@@ -21,29 +23,37 @@ function loginViaPopup(url) {
21
23
  if (!popup) {
22
24
  return;
23
25
  }
24
- const pollTimer = setInterval(() => {
25
- if (popup.closed) {
26
- clearInterval(pollTimer);
26
+ const cleanup = () => {
27
+ window.removeEventListener("message", onMessage);
28
+ clearInterval(pollTimer);
29
+ if (!popup.closed)
30
+ popup.close();
31
+ };
32
+ const onMessage = (event) => {
33
+ var _a;
34
+ if (event.origin !== expectedOrigin)
27
35
  return;
36
+ if (event.source !== popup)
37
+ return;
38
+ if (!((_a = event.data) === null || _a === void 0 ? void 0 : _a.access_token))
39
+ return;
40
+ cleanup();
41
+ // Append the token params to redirectUrl so the app processes them
42
+ // exactly as it would from a normal OAuth callback redirect.
43
+ const callbackUrl = new URL(redirectUrl);
44
+ const { access_token, is_new_user } = event.data;
45
+ callbackUrl.searchParams.set("access_token", access_token);
46
+ if (is_new_user != null) {
47
+ callbackUrl.searchParams.set("is_new_user", String(is_new_user));
28
48
  }
29
- try {
30
- // Accessing popup.location.href throws a cross-origin error while the
31
- // OAuth provider's pages are open that's expected and means the flow
32
- // is still in progress. Once it stops throwing, the popup has landed
33
- // back on our origin with the callback URL (e.g. ?access_token=...).
34
- const callbackUrl = popup.location.href;
35
- if (new URL(callbackUrl).origin === window.location.origin) {
36
- clearInterval(pollTimer);
37
- popup.close();
38
- // Redirect the iframe to the same URL the popup landed on so it
39
- // processes the token from the query params as it normally would.
40
- window.location.href = callbackUrl;
41
- }
42
- }
43
- catch (_a) {
44
- // Still on the OAuth provider's domain — keep polling
45
- }
46
- }, 300);
49
+ window.location.href = callbackUrl.toString();
50
+ };
51
+ // Only used to detect the user closing the popup before auth completes
52
+ const pollTimer = setInterval(() => {
53
+ if (popup.closed)
54
+ cleanup();
55
+ }, 500);
56
+ window.addEventListener("message", onMessage);
47
57
  }
48
58
  /**
49
59
  * Creates the auth module for the Base44 SDK.
@@ -84,13 +94,23 @@ export function createAuthModule(axios, functionsAxiosClient, appId, options) {
84
94
  loginWithProvider(provider, fromUrl = "/") {
85
95
  // Build the full redirect URL
86
96
  const redirectUrl = new URL(fromUrl, window.location.origin).toString();
87
- // Build the provider login URL (google is the default, so no provider path needed)
88
- const providerPath = provider === "google" ? "" : `/${provider}`;
89
- const loginUrl = `${options.appBaseUrl}/api/apps/auth${providerPath}/login?app_id=${appId}&from_url=${encodeURIComponent(redirectUrl)}`;
97
+ const queryParams = `app_id=${appId}&from_url=${encodeURIComponent(redirectUrl)}`;
98
+ // SSO uses a different URL structure with appId in the path
99
+ let authPath;
100
+ if (provider === "sso") {
101
+ authPath = `/apps/${appId}/auth/sso/login`;
102
+ }
103
+ else {
104
+ // Google is the default provider, so no provider path segment needed
105
+ const providerPath = provider === "google" ? "" : `/${provider}`;
106
+ authPath = `/apps/auth${providerPath}/login`;
107
+ }
108
+ const loginUrl = `${options.appBaseUrl}/api${authPath}?${queryParams}`;
90
109
  // On preview/sandbox/checkpoint domains the app runs inside an iframe —
91
110
  // use a popup to avoid OAuth providers blocking iframe navigation.
92
111
  if (isPopupAuthDomain()) {
93
- return loginViaPopup(loginUrl);
112
+ const popupLoginUrl = `${loginUrl}&popup_origin=${encodeURIComponent(window.location.origin)}`;
113
+ return loginViaPopup(popupLoginUrl, redirectUrl, window.location.origin);
94
114
  }
95
115
  // Default: full-page redirect
96
116
  window.location.href = loginUrl;
@@ -185,8 +185,9 @@ export interface AuthModule {
185
185
  * - `'microsoft'` - {@link https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-auth-code-flow | Microsoft OAuth}. Enable Microsoft in your app's authentication settings before specifying this provider.
186
186
  * - `'facebook'` - {@link https://developers.facebook.com/docs/facebook-login | Facebook Login}. Enable Facebook in your app's authentication settings before using.
187
187
  * - `'apple'` - {@link https://developer.apple.com/sign-in-with-apple/ | Sign in with Apple}. Enable Apple in your app's authentication settings before using this provider.
188
+ * - `'sso'` - Enterprise SSO. Enable SSO in your app's authentication settings before using this provider.
188
189
  *
189
- * @param provider - The authentication provider to use: `'google'`, `'microsoft'`, `'facebook'`, or `'apple'`.
190
+ * @param provider - The authentication provider to use: `'google'`, `'microsoft'`, `'facebook'`, `'apple'`, or `'sso'`.
190
191
  * @param fromUrl - URL to redirect to after successful authentication. Defaults to `'/'`.
191
192
  *
192
193
  * @example
@@ -206,6 +207,12 @@ export interface AuthModule {
206
207
  * // Apple
207
208
  * base44.auth.loginWithProvider('apple', '/dashboard');
208
209
  * ```
210
+ *
211
+ * @example
212
+ * ```typescript
213
+ * // SSO
214
+ * base44.auth.loginWithProvider('sso', '/dashboard');
215
+ * ```
209
216
  */
210
217
  loginWithProvider(provider: string, fromUrl?: string): void;
211
218
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44-preview/sdk",
3
- "version": "0.8.19-pr.134.6049a1e",
3
+ "version": "0.8.19-pr.134.76a0e8e",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",