@base44-preview/sdk 0.8.48-pr.282.642a57e → 0.8.48-pr.282.6cbc158
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/client.js +36 -16
- package/dist/utils/embed-session.js +9 -1
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -58,12 +58,16 @@ export function createClient(config) {
|
|
|
58
58
|
const { serverUrl = "https://base44.app", appId, analytics, serviceToken, requiresAuth = false, appBaseUrl, options, functionsVersion, headers: optionalHeaders, } = config;
|
|
59
59
|
// Normalize appBaseUrl to always be a string (empty if not provided or invalid)
|
|
60
60
|
const normalizedAppBaseUrl = typeof appBaseUrl === "string" ? appBaseUrl : "";
|
|
61
|
-
//
|
|
62
|
-
// address bar, in history, or in a shared link.
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
//
|
|
66
|
-
const
|
|
61
|
+
// Always taken off the URL, even outside a frame: a one-time token must not
|
|
62
|
+
// be left in the address bar, in history, or in a shared link.
|
|
63
|
+
const urlOtt = takeEmbedTokenFromUrl();
|
|
64
|
+
// Only redeemed inside a frame. Opened as a normal tab, the same URL falls
|
|
65
|
+
// back to the app's own login, which works there and cannot work in a frame.
|
|
66
|
+
const embedOtt = isFramed() ? urlOtt : null;
|
|
67
|
+
const embedded = Boolean(embedOtt);
|
|
68
|
+
// The passed token is the OTT itself whenever one came in on the URL. That is
|
|
69
|
+
// not this session: it is what the exchange below trades for one.
|
|
70
|
+
const token = urlOtt !== null ? undefined : config.token;
|
|
67
71
|
const headers = {
|
|
68
72
|
...optionalHeaders,
|
|
69
73
|
"X-App-Id": String(appId),
|
|
@@ -119,7 +123,7 @@ export function createClient(config) {
|
|
|
119
123
|
appBaseUrl: normalizedAppBaseUrl,
|
|
120
124
|
serverUrl,
|
|
121
125
|
token,
|
|
122
|
-
embedded
|
|
126
|
+
embedded,
|
|
123
127
|
// The socket carries its token on the handshake, so an identity change
|
|
124
128
|
// only reaches it by opening a new connection — or, on logout, by
|
|
125
129
|
// dropping the one still running as the user who just left.
|
|
@@ -129,16 +133,15 @@ export function createClient(config) {
|
|
|
129
133
|
// requests during construction (notably analytics, which fires an init
|
|
130
134
|
// event whose flush calls auth.me()). Without this, the first User/me
|
|
131
135
|
// request is built before setToken runs and goes out unauthenticated.
|
|
132
|
-
//
|
|
133
|
-
|
|
134
|
-
if (typeof window !== "undefined" && !embedOtt) {
|
|
136
|
+
// Skipped in a frame: the session comes from the exchange below.
|
|
137
|
+
if (typeof window !== "undefined" && !embedded) {
|
|
135
138
|
const accessToken = token || getAccessToken();
|
|
136
139
|
if (accessToken) {
|
|
137
140
|
userAuthModule.setToken(accessToken);
|
|
138
141
|
}
|
|
139
142
|
}
|
|
140
143
|
// Live token for every module; outside a frame it still falls back to storage.
|
|
141
|
-
const getToken = () => { var _a; return (_a = userAuthModule.getToken()) !== null && _a !== void 0 ? _a : (
|
|
144
|
+
const getToken = () => { var _a; return (_a = userAuthModule.getToken()) !== null && _a !== void 0 ? _a : (embedded ? null : getAccessToken()); };
|
|
142
145
|
const socketConfig = {
|
|
143
146
|
serverUrl,
|
|
144
147
|
mountPath: "/ws-user-apps/socket.io/",
|
|
@@ -179,6 +182,21 @@ export function createClient(config) {
|
|
|
179
182
|
console.error("Base44: applying the embedded session failed:", e);
|
|
180
183
|
})
|
|
181
184
|
: Promise.resolve();
|
|
185
|
+
// Everything that can wait for the session does. `aiGateway.connection()`
|
|
186
|
+
// and the agents connect URLs cannot: they hand back a value, not a promise,
|
|
187
|
+
// and a value read now would stay wrong after the session arrives. They warn
|
|
188
|
+
// instead of failing quietly.
|
|
189
|
+
let warnedEarlyRead = false;
|
|
190
|
+
const getTokenNow = () => {
|
|
191
|
+
const sessionToken = getToken();
|
|
192
|
+
if (sessionToken === null && session && !warnedEarlyRead) {
|
|
193
|
+
warnedEarlyRead = true;
|
|
194
|
+
console.warn("Base44: read a token before the embedded session was ready, so it is " +
|
|
195
|
+
"empty. Await a call such as base44.auth.me() before building a " +
|
|
196
|
+
"client or a URL that keeps the token.");
|
|
197
|
+
}
|
|
198
|
+
return sessionToken;
|
|
199
|
+
};
|
|
182
200
|
if (session) {
|
|
183
201
|
// Requests issued during the exchange wait for it. Registered after createAxiosClient's
|
|
184
202
|
// interceptors so it runs first and the anonymous-visitor header sees the Authorization.
|
|
@@ -246,11 +264,13 @@ export function createClient(config) {
|
|
|
246
264
|
getSocket,
|
|
247
265
|
appId,
|
|
248
266
|
serverUrl,
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
267
|
+
getToken: getTokenNow,
|
|
268
|
+
}),
|
|
269
|
+
aiGateway: createAiGatewayModule({
|
|
270
|
+
serverUrl,
|
|
271
|
+
getToken: getTokenNow,
|
|
272
|
+
appId,
|
|
252
273
|
}),
|
|
253
|
-
aiGateway: createAiGatewayModule({ serverUrl, getToken, appId }),
|
|
254
274
|
appLogs: createAppLogsModule(axiosClient, appId),
|
|
255
275
|
app: createAppModule(axiosClient, appId),
|
|
256
276
|
users: createUsersModule(axiosClient, appId),
|
|
@@ -298,7 +318,7 @@ export function createClient(config) {
|
|
|
298
318
|
// The user's token, as on the user-scoped module: the only thing this is
|
|
299
319
|
// read for is the `?token=` on a channel URL handed to that user, which
|
|
300
320
|
// the app's service credential must never end up in.
|
|
301
|
-
getToken,
|
|
321
|
+
getToken: getTokenNow,
|
|
302
322
|
}),
|
|
303
323
|
aiGateway: createAiGatewayModule({
|
|
304
324
|
serverUrl,
|
|
@@ -56,7 +56,15 @@ export function takeEmbedTokenFromUrl() {
|
|
|
56
56
|
}
|
|
57
57
|
/** @internal */
|
|
58
58
|
export function isFramed() {
|
|
59
|
-
|
|
59
|
+
if (typeof window === "undefined") {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
return window.self !== window.top;
|
|
64
|
+
}
|
|
65
|
+
catch (_a) {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
60
68
|
}
|
|
61
69
|
/** @internal */
|
|
62
70
|
export async function exchangeEmbedToken({ serverUrl, appId, ott, fetchImpl = fetch, }) {
|