@base44-preview/sdk 0.8.24-pr.134.9c14bd3 → 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.
- package/dist/modules/auth.js +14 -13
- package/dist/modules/auth.types.d.ts +4 -6
- package/package.json +1 -1
package/dist/modules/auth.js
CHANGED
|
@@ -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,
|
|
9
|
-
*
|
|
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,
|
|
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
|
-
|
|
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
|
|
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,15 +96,13 @@ export interface AuthModuleOptions {
|
|
|
96
96
|
/**
|
|
97
97
|
* Auth state change event types.
|
|
98
98
|
*/
|
|
99
|
-
export type AuthEvent = "SIGNED_IN" | "SIGNED_OUT"
|
|
99
|
+
export type AuthEvent = "SIGNED_IN" | "SIGNED_OUT";
|
|
100
100
|
/**
|
|
101
101
|
* Data passed to auth state change callbacks.
|
|
102
102
|
*/
|
|
103
103
|
export interface AuthEventData {
|
|
104
104
|
/** JWT access token, present on SIGNED_IN and TOKEN_REFRESHED events. */
|
|
105
105
|
access_token?: string;
|
|
106
|
-
/** User data, present on SIGNED_IN when available. */
|
|
107
|
-
user?: User;
|
|
108
106
|
}
|
|
109
107
|
/**
|
|
110
108
|
* Callback for auth state changes.
|
|
@@ -501,7 +499,6 @@ export interface AuthModule {
|
|
|
501
499
|
* Events:
|
|
502
500
|
* - `SIGNED_IN` — fired after a successful login (email/password, OAuth, or popup).
|
|
503
501
|
* - `SIGNED_OUT` — fired after logout.
|
|
504
|
-
* - `TOKEN_REFRESHED` — fired when `setToken` is called while already authenticated.
|
|
505
502
|
*
|
|
506
503
|
* Returns an unsubscribe function. Call it to stop receiving events.
|
|
507
504
|
*
|
|
@@ -512,9 +509,10 @@ export interface AuthModule {
|
|
|
512
509
|
* ```typescript
|
|
513
510
|
* // In a React AuthContext provider
|
|
514
511
|
* useEffect(() => {
|
|
515
|
-
* const unsubscribe = base44.auth.onAuthStateChange((event, data) => {
|
|
512
|
+
* const unsubscribe = base44.auth.onAuthStateChange(async (event, data) => {
|
|
516
513
|
* if (event === 'SIGNED_IN') {
|
|
517
|
-
*
|
|
514
|
+
* const user = await base44.auth.me();
|
|
515
|
+
* setUser(user);
|
|
518
516
|
* } else if (event === 'SIGNED_OUT') {
|
|
519
517
|
* setUser(null);
|
|
520
518
|
* }
|