@base44-preview/sdk 0.8.54-pr.264.92c17fa → 0.8.54-pr.264.cfcaa05
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 +20 -5
- package/package.json +1 -1
package/dist/modules/auth.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { removeAccessToken } from "../utils/auth-utils.js";
|
|
1
|
+
import { getAccessToken, removeAccessToken } from "../utils/auth-utils.js";
|
|
2
2
|
import { resetAnalyticsSessionContext } from "./analytics.js";
|
|
3
3
|
function isInsideIframe() {
|
|
4
4
|
if (typeof window === "undefined")
|
|
@@ -59,6 +59,16 @@ function removeStoredAccessTokens() {
|
|
|
59
59
|
removeAccessToken({});
|
|
60
60
|
removeAccessToken({ storageKey: "token" });
|
|
61
61
|
}
|
|
62
|
+
const isStoredAccessToken = (authorization) => authorization ===
|
|
63
|
+
`Bearer ${getAccessToken({ saveToStorage: false, removeFromUrl: false })}`;
|
|
64
|
+
// A rejected token left in storage boots the next page with it, and an app
|
|
65
|
+
// whose own /login does that loops 401 → /login. A token stored since the
|
|
66
|
+
// request went out (a login in this or another tab) is not the rejected one.
|
|
67
|
+
function clearTokenOnRejection(error, sentAuthorization) {
|
|
68
|
+
if (error.status === 401 && isStoredAccessToken(sentAuthorization)) {
|
|
69
|
+
removeStoredAccessTokens();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
62
72
|
/**
|
|
63
73
|
* Creates the auth module for the Base44 SDK.
|
|
64
74
|
*
|
|
@@ -94,7 +104,14 @@ export function createAuthModule(axios, functionsAxiosClient, appId, options) {
|
|
|
94
104
|
},
|
|
95
105
|
// Get current user information
|
|
96
106
|
async me() {
|
|
97
|
-
const
|
|
107
|
+
const sentAuthorization = axios.defaults.headers.common["Authorization"];
|
|
108
|
+
const request = pendingMe !== null && pendingMe !== void 0 ? pendingMe : axios
|
|
109
|
+
.get(`/apps/${appId}/entities/User/me`)
|
|
110
|
+
.catch((error) => {
|
|
111
|
+
clearTokenOnRejection(error, sentAuthorization);
|
|
112
|
+
throw error;
|
|
113
|
+
})
|
|
114
|
+
.finally(() => {
|
|
98
115
|
// Only retire this request if it is still the shared one. An identity
|
|
99
116
|
// change mid-flight clears `pendingMe` and the next caller starts a
|
|
100
117
|
// fresh request; an unconditional clear here would retire that newer
|
|
@@ -121,9 +138,7 @@ export function createAuthModule(axios, functionsAxiosClient, appId, options) {
|
|
|
121
138
|
: window.location.href;
|
|
122
139
|
// Build the login URL
|
|
123
140
|
const loginUrl = `${options.appBaseUrl}/login?from_url=${encodeURIComponent(redirectUrl)}`;
|
|
124
|
-
//
|
|
125
|
-
// or a rejected token redirects to /login forever.
|
|
126
|
-
removeStoredAccessTokens();
|
|
141
|
+
// Redirect to the login page
|
|
127
142
|
window.location.href = loginUrl;
|
|
128
143
|
},
|
|
129
144
|
// Redirects the user to a provider's login page
|