@base44-preview/sdk 0.8.24-pr.134.4e99e37 → 0.8.24-pr.134.cd7b8e1

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,15 @@ function isInsideIframe() {
5
5
  }
6
6
  /**
7
7
  * Opens a URL in a centered popup and waits for the backend to postMessage
8
- * the auth result back. On success, calls onToken so the SDK can set the
9
- * token and fire auth state change events no page redirect needed.
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.
10
11
  *
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).
12
14
  * @param expectedOrigin - The origin we expect the postMessage to come from.
13
- * @param onToken - Callback invoked with the access_token when auth completes.
14
15
  */
15
- function loginViaPopup(url, expectedOrigin, onToken) {
16
+ function loginViaPopup(url, redirectUrl, expectedOrigin) {
16
17
  const width = 500;
17
18
  const height = 600;
18
19
  const left = Math.round(window.screenX + (window.outerWidth - width) / 2);
@@ -36,7 +37,13 @@ function loginViaPopup(url, expectedOrigin, onToken) {
36
37
  if (!((_a = event.data) === null || _a === void 0 ? void 0 : _a.access_token))
37
38
  return;
38
39
  cleanup();
39
- onToken(event.data.access_token);
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();
40
47
  };
41
48
  // Only used to detect the user closing the popup before auth completes
42
49
  const pollTimer = setInterval(() => {
@@ -57,7 +64,6 @@ function loginViaPopup(url, expectedOrigin, onToken) {
57
64
  */
58
65
  export function createAuthModule(axios, functionsAxiosClient, appId, options) {
59
66
  const listeners = new Set();
60
- let hasToken = false;
61
67
  function notify(event, data = {}) {
62
68
  listeners.forEach((cb) => cb(event, data));
63
69
  }
@@ -105,9 +111,7 @@ export function createAuthModule(axios, functionsAxiosClient, appId, options) {
105
111
  // blocking iframe navigation.
106
112
  if (isInsideIframe()) {
107
113
  const popupLoginUrl = `${loginUrl}&popup_origin=${encodeURIComponent(window.location.origin)}`;
108
- return loginViaPopup(popupLoginUrl, window.location.origin, (token) => {
109
- this.setToken(token);
110
- });
114
+ return loginViaPopup(popupLoginUrl, redirectUrl, window.location.origin);
111
115
  }
112
116
  // Default: full-page redirect
113
117
  window.location.href = loginUrl;
@@ -116,7 +120,6 @@ export function createAuthModule(axios, functionsAxiosClient, appId, options) {
116
120
  logout(redirectUrl) {
117
121
  // Remove token from axios headers (always do this)
118
122
  delete axios.defaults.headers.common["Authorization"];
119
- hasToken = false;
120
123
  notify("SIGNED_OUT");
121
124
  // Only do the rest if in a browser environment
122
125
  if (typeof window !== "undefined") {
@@ -142,7 +145,6 @@ export function createAuthModule(axios, functionsAxiosClient, appId, options) {
142
145
  setToken(token, saveToStorage = true) {
143
146
  if (!token)
144
147
  return;
145
- const event = hasToken ? "TOKEN_REFRESHED" : "SIGNED_IN";
146
148
  // handle token change for axios clients
147
149
  axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
148
150
  functionsAxiosClient.defaults.headers.common["Authorization"] = `Bearer ${token}`;
@@ -159,8 +161,6 @@ export function createAuthModule(axios, functionsAxiosClient, appId, options) {
159
161
  console.error("Failed to save token to localStorage:", e);
160
162
  }
161
163
  }
162
- hasToken = true;
163
- notify(event, { access_token: token });
164
164
  },
165
165
  // Login using username and password
166
166
  async loginViaEmailPassword(email, password, turnstileToken) {
@@ -174,6 +174,7 @@ export function createAuthModule(axios, functionsAxiosClient, appId, options) {
174
174
  const { access_token, user } = response;
175
175
  if (access_token) {
176
176
  this.setToken(access_token);
177
+ notify("SIGNED_IN", { access_token });
177
178
  }
178
179
  return {
179
180
  access_token,
@@ -96,7 +96,7 @@ export interface AuthModuleOptions {
96
96
  /**
97
97
  * Auth state change event types.
98
98
  */
99
- export type AuthEvent = "SIGNED_IN" | "SIGNED_OUT" | "TOKEN_REFRESHED";
99
+ export type AuthEvent = "SIGNED_IN" | "SIGNED_OUT";
100
100
  /**
101
101
  * Data passed to auth state change callbacks.
102
102
  */
@@ -499,7 +499,6 @@ export interface AuthModule {
499
499
  * Events:
500
500
  * - `SIGNED_IN` — fired after a successful login (email/password, OAuth, or popup).
501
501
  * - `SIGNED_OUT` — fired after logout.
502
- * - `TOKEN_REFRESHED` — fired when `setToken` is called while already authenticated.
503
502
  *
504
503
  * Returns an unsubscribe function. Call it to stop receiving events.
505
504
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44-preview/sdk",
3
- "version": "0.8.24-pr.134.4e99e37",
3
+ "version": "0.8.24-pr.134.cd7b8e1",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",