@stackframe/stack 2.4.17 → 2.4.20
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/CHANGELOG.md +26 -0
- package/dist/components/credential-sign-in.js +1 -1
- package/dist/components/credential-sign-in.js.map +1 -1
- package/dist/components/credential-sign-up.js +1 -1
- package/dist/components/credential-sign-up.js.map +1 -1
- package/dist/components/forgot-password.js +1 -1
- package/dist/components/forgot-password.js.map +1 -1
- package/dist/components/magic-link-sign-in.js +1 -1
- package/dist/components/magic-link-sign-in.js.map +1 -1
- package/dist/components/maybe-full-page.js +1 -1
- package/dist/components/maybe-full-page.js.map +1 -1
- package/dist/components/password-reset-inner.js +1 -1
- package/dist/components/password-reset-inner.js.map +1 -1
- package/dist/components/user-avatar.d.mts +1 -0
- package/dist/components/user-avatar.d.ts +1 -0
- package/dist/components/user-avatar.js +1 -1
- package/dist/components/user-avatar.js.map +1 -1
- package/dist/components/user-button.js +4 -4
- package/dist/components/user-button.js.map +1 -1
- package/dist/components-core/button.js.map +1 -1
- package/dist/components-core/index.d.mts +1 -1
- package/dist/components-core/index.d.ts +1 -1
- package/dist/components-core-joy/button.js +1 -1
- package/dist/components-core-joy/button.js.map +1 -1
- package/dist/components-page/account-settings.js +1 -1
- package/dist/components-page/account-settings.js.map +1 -1
- package/dist/components-page/stack-handler.d.mts +1 -0
- package/dist/components-page/stack-handler.d.ts +1 -0
- package/dist/esm/components/credential-sign-in.js +2 -2
- package/dist/esm/components/credential-sign-in.js.map +1 -1
- package/dist/esm/components/credential-sign-up.js +2 -2
- package/dist/esm/components/credential-sign-up.js.map +1 -1
- package/dist/esm/components/forgot-password.js +2 -2
- package/dist/esm/components/forgot-password.js.map +1 -1
- package/dist/esm/components/magic-link-sign-in.js +2 -2
- package/dist/esm/components/magic-link-sign-in.js.map +1 -1
- package/dist/esm/components/maybe-full-page.js +1 -1
- package/dist/esm/components/maybe-full-page.js.map +1 -1
- package/dist/esm/components/password-reset-inner.js +2 -2
- package/dist/esm/components/password-reset-inner.js.map +1 -1
- package/dist/esm/components/user-avatar.js +1 -1
- package/dist/esm/components/user-avatar.js.map +1 -1
- package/dist/esm/components/user-button.js +5 -5
- package/dist/esm/components/user-button.js.map +1 -1
- package/dist/esm/components-core/button.js.map +1 -1
- package/dist/esm/components-core-joy/button.js +1 -1
- package/dist/esm/components-core-joy/button.js.map +1 -1
- package/dist/esm/components-page/account-settings.js +1 -1
- package/dist/esm/components-page/account-settings.js.map +1 -1
- package/dist/esm/lib/auth.js +2 -3
- package/dist/esm/lib/auth.js.map +1 -1
- package/dist/esm/lib/stack-app.js +305 -192
- package/dist/esm/lib/stack-app.js.map +1 -1
- package/dist/esm/providers/stack-provider-client.js +2 -1
- package/dist/esm/providers/stack-provider-client.js.map +1 -1
- package/dist/esm/providers/styled-components-registry.js +2 -1
- package/dist/esm/providers/styled-components-registry.js.map +1 -1
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/lib/auth.d.mts +5 -3
- package/dist/lib/auth.d.ts +5 -3
- package/dist/lib/auth.js +2 -3
- package/dist/lib/auth.js.map +1 -1
- package/dist/lib/hooks.d.mts +1 -0
- package/dist/lib/hooks.d.ts +1 -0
- package/dist/lib/stack-app.d.mts +98 -18
- package/dist/lib/stack-app.d.ts +98 -18
- package/dist/lib/stack-app.js +303 -190
- package/dist/lib/stack-app.js.map +1 -1
- package/dist/providers/component-provider.d.mts +2 -2
- package/dist/providers/component-provider.d.ts +2 -2
- package/dist/providers/stack-provider-client.d.mts +1 -0
- package/dist/providers/stack-provider-client.d.ts +1 -0
- package/dist/providers/stack-provider-client.js +2 -1
- package/dist/providers/stack-provider-client.js.map +1 -1
- package/dist/providers/stack-provider.d.mts +1 -0
- package/dist/providers/stack-provider.d.ts +1 -0
- package/dist/providers/styled-components-registry.js +2 -1
- package/dist/providers/styled-components-registry.js.map +1 -1
- package/package.json +3 -3
- package/dist/esm/utils/next.js +0 -8
- package/dist/esm/utils/next.js.map +0 -1
- package/dist/utils/next.d.mts +0 -3
- package/dist/utils/next.d.ts +0 -3
- package/dist/utils/next.js +0 -33
- package/dist/utils/next.js.map +0 -1
package/dist/lib/stack-app.js
CHANGED
|
@@ -45,7 +45,7 @@ var import_results = require("@stackframe/stack-shared/dist/utils/results");
|
|
|
45
45
|
var import_react2 = require("@stackframe/stack-shared/dist/utils/react");
|
|
46
46
|
var import_stores = require("@stackframe/stack-shared/dist/utils/stores");
|
|
47
47
|
var import_clientInterface = require("@stackframe/stack-shared/dist/interface/clientInterface");
|
|
48
|
-
var
|
|
48
|
+
var import_env = require("@stackframe/stack-shared/src/utils/env");
|
|
49
49
|
var import_auth = require("./auth");
|
|
50
50
|
var NextNavigationUnscrambled = __toESM(require("next/navigation"));
|
|
51
51
|
var import_url = require("../utils/url");
|
|
@@ -56,8 +56,10 @@ var import_react3 = require("@stackframe/stack-shared/dist/utils/react");
|
|
|
56
56
|
var import_compile_time = require("@stackframe/stack-shared/dist/utils/compile-time");
|
|
57
57
|
var import_stack_sc = require("@stackframe/stack-sc");
|
|
58
58
|
var cookie = __toESM(require("cookie"));
|
|
59
|
+
var import_sessions = require("@stackframe/stack-shared/dist/sessions");
|
|
60
|
+
var import_use_trigger = require("@stackframe/stack-shared/dist/hooks/use-trigger");
|
|
59
61
|
var NextNavigation = (0, import_compile_time.scrambleDuringCompileTime)(NextNavigationUnscrambled);
|
|
60
|
-
var clientVersion = "js @stackframe/stack@2.4.
|
|
62
|
+
var clientVersion = "js @stackframe/stack@2.4.20";
|
|
61
63
|
function permissionDefinitionScopeToType(scope) {
|
|
62
64
|
return { "any-team": "team", "specific-team": "team", "global": "global" }[scope.type];
|
|
63
65
|
}
|
|
@@ -98,45 +100,13 @@ function getDefaultBaseUrl() {
|
|
|
98
100
|
}
|
|
99
101
|
var defaultBaseUrl = "https://app.stack-auth.com";
|
|
100
102
|
function createEmptyTokenStore() {
|
|
101
|
-
return new import_stores.
|
|
103
|
+
return new import_stores.Store({
|
|
102
104
|
refreshToken: null,
|
|
103
105
|
accessToken: null
|
|
104
106
|
});
|
|
105
107
|
}
|
|
106
|
-
var cookieTokenStore = null;
|
|
107
|
-
var cookieTokenStoreInitializer = () => {
|
|
108
|
-
if (!(0, import_next.isClient)()) {
|
|
109
|
-
throw new Error("Cannot use cookie token store on the server!");
|
|
110
|
-
}
|
|
111
|
-
if (cookieTokenStore === null) {
|
|
112
|
-
cookieTokenStore = new import_stores.AsyncStore();
|
|
113
|
-
let hasSucceededInWriting = true;
|
|
114
|
-
setInterval(() => {
|
|
115
|
-
if (hasSucceededInWriting) {
|
|
116
|
-
const newValue = {
|
|
117
|
-
refreshToken: (0, import_cookie.getCookie)("stack-refresh"),
|
|
118
|
-
accessToken: (0, import_cookie.getCookie)("stack-access")
|
|
119
|
-
};
|
|
120
|
-
const res = cookieTokenStore.get();
|
|
121
|
-
if (res.status !== "ok" || res.data.refreshToken !== newValue.refreshToken || res.data.accessToken !== newValue.accessToken) {
|
|
122
|
-
cookieTokenStore.set(newValue);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}, 10);
|
|
126
|
-
cookieTokenStore.onChange((value) => {
|
|
127
|
-
try {
|
|
128
|
-
(0, import_cookie.setOrDeleteCookie)("stack-refresh", value.refreshToken, { maxAge: 60 * 60 * 24 * 365 });
|
|
129
|
-
(0, import_cookie.setOrDeleteCookie)("stack-access", value.accessToken, { maxAge: 60 * 60 * 24 });
|
|
130
|
-
hasSucceededInWriting = true;
|
|
131
|
-
} catch (e) {
|
|
132
|
-
hasSucceededInWriting = false;
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
return cookieTokenStore;
|
|
137
|
-
};
|
|
138
108
|
var loadingSentinel = Symbol("stackAppCacheLoadingSentinel");
|
|
139
|
-
function
|
|
109
|
+
function useAsyncCache(cache, dependencies, caller) {
|
|
140
110
|
(0, import_react2.suspendIfSsr)(caller);
|
|
141
111
|
const subscribe = (0, import_react.useCallback)((cb) => {
|
|
142
112
|
const { unsubscribe } = cache.onChange(dependencies, () => cb());
|
|
@@ -160,17 +130,13 @@ var createCache = (fetcher) => {
|
|
|
160
130
|
{}
|
|
161
131
|
);
|
|
162
132
|
};
|
|
163
|
-
var
|
|
133
|
+
var createCacheBySession = (fetcher) => {
|
|
164
134
|
return new import_caches.AsyncCache(
|
|
165
|
-
async ([
|
|
135
|
+
async ([session, ...extraDependencies]) => await fetcher(session, extraDependencies),
|
|
166
136
|
{
|
|
167
|
-
onSubscribe: ([
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
return;
|
|
171
|
-
refresh();
|
|
172
|
-
});
|
|
173
|
-
return () => handlerObj.unsubscribe();
|
|
137
|
+
onSubscribe: ([session], refresh) => {
|
|
138
|
+
const handler = session.onInvalidate(() => refresh());
|
|
139
|
+
return () => handler.unsubscribe();
|
|
174
140
|
}
|
|
175
141
|
}
|
|
176
142
|
);
|
|
@@ -197,7 +163,7 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
197
163
|
}
|
|
198
164
|
numberOfAppsCreated++;
|
|
199
165
|
if (numberOfAppsCreated > 10) {
|
|
200
|
-
console.warn(`You have created more than 10 Stack apps (${numberOfAppsCreated}). This is usually a sign of a memory leak.
|
|
166
|
+
(process.env.NODE_ENV === "development" ? console.log : console.warn)(`You have created more than 10 Stack apps (${numberOfAppsCreated}). This is usually a sign of a memory leak, but can sometimes be caused by hot reload of your tech stack. In production, make sure to minimize the number of Stack apps per page (usually, one per project).`);
|
|
201
167
|
}
|
|
202
168
|
}
|
|
203
169
|
_uniqueIdentifier = void 0;
|
|
@@ -205,24 +171,24 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
205
171
|
_tokenStoreInit;
|
|
206
172
|
_urlOptions;
|
|
207
173
|
__DEMO_ENABLE_SLIGHT_FETCH_DELAY = false;
|
|
208
|
-
_currentUserCache =
|
|
174
|
+
_currentUserCache = createCacheBySession(async (session) => {
|
|
209
175
|
if (this.__DEMO_ENABLE_SLIGHT_FETCH_DELAY) {
|
|
210
176
|
await (0, import_promises.wait)(2e3);
|
|
211
177
|
}
|
|
212
|
-
const user = await this._interface.getClientUserByToken(
|
|
178
|
+
const user = await this._interface.getClientUserByToken(session);
|
|
213
179
|
return import_results.Result.or(user, null);
|
|
214
180
|
});
|
|
215
181
|
_currentProjectCache = createCache(async () => {
|
|
216
182
|
return import_results.Result.orThrow(await this._interface.getClientProject());
|
|
217
183
|
});
|
|
218
|
-
_ownedProjectsCache =
|
|
219
|
-
return await this._interface.listProjects(
|
|
184
|
+
_ownedProjectsCache = createCacheBySession(async (session) => {
|
|
185
|
+
return await this._interface.listProjects(session);
|
|
220
186
|
});
|
|
221
|
-
_currentUserPermissionsCache =
|
|
222
|
-
return await this._interface.listClientUserTeamPermissions({ teamId, type, direct },
|
|
187
|
+
_currentUserPermissionsCache = createCacheBySession(async (session, [teamId, type, direct]) => {
|
|
188
|
+
return await this._interface.listClientUserTeamPermissions({ teamId, type, direct }, session);
|
|
223
189
|
});
|
|
224
|
-
_currentUserTeamsCache =
|
|
225
|
-
return await this._interface.listClientUserTeams(
|
|
190
|
+
_currentUserTeamsCache = createCacheBySession(async (session) => {
|
|
191
|
+
return await this._interface.listClientUserTeams(session);
|
|
226
192
|
});
|
|
227
193
|
_initUniqueIdentifier() {
|
|
228
194
|
if (!this._uniqueIdentifier) {
|
|
@@ -245,26 +211,73 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
245
211
|
return this._uniqueIdentifier;
|
|
246
212
|
}
|
|
247
213
|
_memoryTokenStore = createEmptyTokenStore();
|
|
248
|
-
_requestTokenStores = /* @__PURE__ */ new
|
|
249
|
-
|
|
214
|
+
_requestTokenStores = /* @__PURE__ */ new WeakMap();
|
|
215
|
+
_storedCookieTokenStore = null;
|
|
216
|
+
get _refreshTokenCookieName() {
|
|
217
|
+
return `stack-refresh-${this.projectId}`;
|
|
218
|
+
}
|
|
219
|
+
get _accessTokenCookieName() {
|
|
220
|
+
return `stack-access`;
|
|
221
|
+
}
|
|
222
|
+
_getCookieTokenStore() {
|
|
223
|
+
if (!(0, import_env.isBrowserLike)()) {
|
|
224
|
+
throw new Error("Cannot use cookie token store on the server!");
|
|
225
|
+
}
|
|
226
|
+
if (this._storedCookieTokenStore === null) {
|
|
227
|
+
const getCurrentValue = (old) => ({
|
|
228
|
+
refreshToken: (0, import_cookie.getCookie)(this._refreshTokenCookieName) ?? (0, import_cookie.getCookie)("stack-refresh"),
|
|
229
|
+
// keep old cookie name for backwards-compatibility
|
|
230
|
+
// if there is an access token in memory already, don't update the access token based on cookies (access token
|
|
231
|
+
// cookies may be set by another project on the same domain)
|
|
232
|
+
// see the comment in _accessTokenCookieName for more information
|
|
233
|
+
accessToken: old === null ? (0, import_cookie.getCookie)(this._accessTokenCookieName) : old.accessToken
|
|
234
|
+
});
|
|
235
|
+
this._storedCookieTokenStore = new import_stores.Store(getCurrentValue(null));
|
|
236
|
+
let hasSucceededInWriting = true;
|
|
237
|
+
setInterval(() => {
|
|
238
|
+
if (hasSucceededInWriting) {
|
|
239
|
+
const oldValue = this._storedCookieTokenStore.get();
|
|
240
|
+
const currentValue = getCurrentValue(oldValue);
|
|
241
|
+
if (!(0, import_objects.deepPlainEquals)(currentValue, oldValue)) {
|
|
242
|
+
this._storedCookieTokenStore.set(currentValue);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}, 100);
|
|
246
|
+
this._storedCookieTokenStore.onChange((value) => {
|
|
247
|
+
try {
|
|
248
|
+
(0, import_cookie.setOrDeleteCookie)(this._refreshTokenCookieName, value.refreshToken, { maxAge: 60 * 60 * 24 * 365 });
|
|
249
|
+
(0, import_cookie.setOrDeleteCookie)(this._accessTokenCookieName, value.accessToken, { maxAge: 60 * 60 * 24 });
|
|
250
|
+
hasSucceededInWriting = true;
|
|
251
|
+
} catch (e) {
|
|
252
|
+
if (!(0, import_env.isBrowserLike)()) {
|
|
253
|
+
hasSucceededInWriting = false;
|
|
254
|
+
} else {
|
|
255
|
+
throw e;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
return this._storedCookieTokenStore;
|
|
261
|
+
}
|
|
262
|
+
_getOrCreateTokenStore(overrideTokenStoreInit) {
|
|
250
263
|
const tokenStoreInit = overrideTokenStoreInit === void 0 ? this._tokenStoreInit : overrideTokenStoreInit;
|
|
251
264
|
switch (tokenStoreInit) {
|
|
252
265
|
case "cookie": {
|
|
253
|
-
return
|
|
266
|
+
return this._getCookieTokenStore();
|
|
254
267
|
}
|
|
255
268
|
case "nextjs-cookie": {
|
|
256
|
-
if ((0,
|
|
257
|
-
return
|
|
269
|
+
if ((0, import_env.isBrowserLike)()) {
|
|
270
|
+
return this._getCookieTokenStore();
|
|
258
271
|
} else {
|
|
259
|
-
const store = new import_stores.
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
accessToken: (0, import_cookie.getCookie)(
|
|
272
|
+
const store = new import_stores.Store({
|
|
273
|
+
refreshToken: (0, import_cookie.getCookie)(this._refreshTokenCookieName) ?? (0, import_cookie.getCookie)("stack-refresh"),
|
|
274
|
+
// keep old cookie name for backwards-compatibility
|
|
275
|
+
accessToken: (0, import_cookie.getCookie)(this._accessTokenCookieName)
|
|
263
276
|
});
|
|
264
277
|
store.onChange((value) => {
|
|
265
278
|
try {
|
|
266
|
-
(0, import_cookie.setOrDeleteCookie)(
|
|
267
|
-
(0, import_cookie.setOrDeleteCookie)(
|
|
279
|
+
(0, import_cookie.setOrDeleteCookie)(this._refreshTokenCookieName, value.refreshToken, { maxAge: 60 * 60 * 24 * 365 });
|
|
280
|
+
(0, import_cookie.setOrDeleteCookie)(this._accessTokenCookieName, value.accessToken, { maxAge: 60 * 60 * 24 });
|
|
268
281
|
} catch (e) {
|
|
269
282
|
}
|
|
270
283
|
});
|
|
@@ -274,26 +287,102 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
274
287
|
case "memory": {
|
|
275
288
|
return this._memoryTokenStore;
|
|
276
289
|
}
|
|
277
|
-
case null: {
|
|
278
|
-
return createEmptyTokenStore();
|
|
279
|
-
}
|
|
280
290
|
default: {
|
|
281
|
-
if (tokenStoreInit
|
|
291
|
+
if (tokenStoreInit === null) {
|
|
292
|
+
return createEmptyTokenStore();
|
|
293
|
+
} else if (typeof tokenStoreInit === "object" && "headers" in tokenStoreInit) {
|
|
282
294
|
if (this._requestTokenStores.has(tokenStoreInit))
|
|
283
295
|
return this._requestTokenStores.get(tokenStoreInit);
|
|
296
|
+
const stackAuthHeader = tokenStoreInit.headers.get("x-stack-auth");
|
|
297
|
+
if (stackAuthHeader) {
|
|
298
|
+
let parsed2;
|
|
299
|
+
try {
|
|
300
|
+
parsed2 = JSON.parse(stackAuthHeader);
|
|
301
|
+
if (typeof parsed2 !== "object")
|
|
302
|
+
throw new Error("x-stack-auth header must be a JSON object");
|
|
303
|
+
if (parsed2 === null)
|
|
304
|
+
throw new Error("x-stack-auth header must not be null");
|
|
305
|
+
} catch (e) {
|
|
306
|
+
throw new Error(`Invalid x-stack-auth header: ${stackAuthHeader}`, { cause: e });
|
|
307
|
+
}
|
|
308
|
+
return this._getOrCreateTokenStore({
|
|
309
|
+
accessToken: parsed2.accessToken ?? null,
|
|
310
|
+
refreshToken: parsed2.refreshToken ?? null
|
|
311
|
+
});
|
|
312
|
+
}
|
|
284
313
|
const cookieHeader = tokenStoreInit.headers.get("cookie");
|
|
285
314
|
const parsed = cookie.parse(cookieHeader || "");
|
|
286
|
-
const res = new import_stores.
|
|
287
|
-
refreshToken: parsed["stack-refresh"] || null,
|
|
288
|
-
|
|
315
|
+
const res = new import_stores.Store({
|
|
316
|
+
refreshToken: parsed[this._refreshTokenCookieName] || parsed["stack-refresh"] || null,
|
|
317
|
+
// keep old cookie name for backwards-compatibility
|
|
318
|
+
accessToken: parsed[this._accessTokenCookieName] || null
|
|
289
319
|
});
|
|
290
320
|
this._requestTokenStores.set(tokenStoreInit, res);
|
|
291
321
|
return res;
|
|
322
|
+
} else if ("accessToken" in tokenStoreInit || "refreshToken" in tokenStoreInit) {
|
|
323
|
+
return new import_stores.Store({
|
|
324
|
+
refreshToken: tokenStoreInit.refreshToken,
|
|
325
|
+
accessToken: tokenStoreInit.accessToken
|
|
326
|
+
});
|
|
292
327
|
}
|
|
293
328
|
throw new Error(`Invalid token store ${tokenStoreInit}`);
|
|
294
329
|
}
|
|
295
330
|
}
|
|
296
331
|
}
|
|
332
|
+
/**
|
|
333
|
+
* A map from token stores and session keys to sessions.
|
|
334
|
+
*
|
|
335
|
+
* This isn't just a map from session keys to sessions for two reasons:
|
|
336
|
+
*
|
|
337
|
+
* - So we can garbage-collect Session objects when the token store is garbage-collected
|
|
338
|
+
* - So different token stores are separated and don't leak information between each other, eg. if the same user sends two requests to the same server they should get a different session object
|
|
339
|
+
*/
|
|
340
|
+
_sessionsByTokenStoreAndSessionKey = /* @__PURE__ */ new WeakMap();
|
|
341
|
+
_getSessionFromTokenStore(tokenStore) {
|
|
342
|
+
const tokenObj = tokenStore.get();
|
|
343
|
+
const sessionKey = import_sessions.InternalSession.calculateSessionKey(tokenObj);
|
|
344
|
+
const existing = sessionKey ? this._sessionsByTokenStoreAndSessionKey.get(tokenStore)?.get(sessionKey) : null;
|
|
345
|
+
if (existing)
|
|
346
|
+
return existing;
|
|
347
|
+
const session = this._interface.createSession({
|
|
348
|
+
refreshToken: tokenObj.refreshToken,
|
|
349
|
+
accessToken: tokenObj.accessToken
|
|
350
|
+
});
|
|
351
|
+
session.onAccessTokenChange((newAccessToken) => {
|
|
352
|
+
tokenStore.update((old) => ({
|
|
353
|
+
...old,
|
|
354
|
+
accessToken: newAccessToken?.token ?? null
|
|
355
|
+
}));
|
|
356
|
+
});
|
|
357
|
+
session.onInvalidate(() => {
|
|
358
|
+
tokenStore.update((old) => ({
|
|
359
|
+
...old,
|
|
360
|
+
accessToken: null,
|
|
361
|
+
refreshToken: null
|
|
362
|
+
}));
|
|
363
|
+
});
|
|
364
|
+
let sessionsBySessionKey = this._sessionsByTokenStoreAndSessionKey.get(tokenStore) ?? /* @__PURE__ */ new Map();
|
|
365
|
+
this._sessionsByTokenStoreAndSessionKey.set(tokenStore, sessionsBySessionKey);
|
|
366
|
+
sessionsBySessionKey.set(sessionKey, session);
|
|
367
|
+
return session;
|
|
368
|
+
}
|
|
369
|
+
_getSession(overrideTokenStoreInit) {
|
|
370
|
+
const tokenStore = this._getOrCreateTokenStore(overrideTokenStoreInit);
|
|
371
|
+
return this._getSessionFromTokenStore(tokenStore);
|
|
372
|
+
}
|
|
373
|
+
_useSession(overrideTokenStoreInit) {
|
|
374
|
+
const tokenStore = this._getOrCreateTokenStore(overrideTokenStoreInit);
|
|
375
|
+
const subscribe = (0, import_react.useCallback)((cb) => {
|
|
376
|
+
const { unsubscribe } = tokenStore.onChange(() => cb());
|
|
377
|
+
return unsubscribe;
|
|
378
|
+
}, [tokenStore]);
|
|
379
|
+
const getSnapshot = (0, import_react.useCallback)(() => this._getSessionFromTokenStore(tokenStore), [tokenStore]);
|
|
380
|
+
return import_react.default.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
381
|
+
}
|
|
382
|
+
async _signInToAccountWithTokens(tokens) {
|
|
383
|
+
const tokenStore = this._getOrCreateTokenStore();
|
|
384
|
+
tokenStore.set(tokens);
|
|
385
|
+
}
|
|
297
386
|
_hasPersistentTokenStore(overrideTokenStoreInit) {
|
|
298
387
|
return (overrideTokenStoreInit !== void 0 ? overrideTokenStoreInit : this._tokenStoreInit) !== null;
|
|
299
388
|
}
|
|
@@ -373,24 +462,25 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
373
462
|
});
|
|
374
463
|
},
|
|
375
464
|
async listTeams() {
|
|
376
|
-
const teams = await app._currentUserTeamsCache.getOrWait([app.
|
|
465
|
+
const teams = await app._currentUserTeamsCache.getOrWait([app._getSession()], "write-only");
|
|
377
466
|
return teams.map((json2) => app._teamFromJson(json2));
|
|
378
467
|
},
|
|
379
468
|
useTeams() {
|
|
380
|
-
const
|
|
469
|
+
const session = app._useSession();
|
|
470
|
+
const teams = useAsyncCache(app._currentUserTeamsCache, [session], "user.useTeams()");
|
|
381
471
|
return (0, import_react.useMemo)(() => teams.map((json2) => app._teamFromJson(json2)), [teams]);
|
|
382
472
|
},
|
|
383
473
|
onTeamsChange(callback) {
|
|
384
|
-
return app._currentUserTeamsCache.onChange([app.
|
|
474
|
+
return app._currentUserTeamsCache.onChange([app._getSession()], (value, oldValue) => {
|
|
385
475
|
callback(value.map((json2) => app._teamFromJson(json2)), oldValue?.map((json2) => app._teamFromJson(json2)));
|
|
386
476
|
});
|
|
387
477
|
},
|
|
388
478
|
async listPermissions(scope, options) {
|
|
389
|
-
const permissions = await app._currentUserPermissionsCache.getOrWait([app.
|
|
479
|
+
const permissions = await app._currentUserPermissionsCache.getOrWait([app._getSession(), scope.id, "team", !!options?.direct], "write-only");
|
|
390
480
|
return permissions.map((json2) => app._permissionFromJson(json2));
|
|
391
481
|
},
|
|
392
482
|
usePermissions(scope, options) {
|
|
393
|
-
const permissions =
|
|
483
|
+
const permissions = useAsyncCache(app._currentUserPermissionsCache, [app._getSession(), scope.id, "team", !!options?.direct], "user.usePermissions()");
|
|
394
484
|
return (0, import_react.useMemo)(() => permissions.map((json2) => app._permissionFromJson(json2)), [permissions]);
|
|
395
485
|
},
|
|
396
486
|
usePermission(scope, permissionId) {
|
|
@@ -418,30 +508,36 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
418
508
|
displayName: json.displayName
|
|
419
509
|
};
|
|
420
510
|
}
|
|
421
|
-
_currentUserFromJson(json,
|
|
511
|
+
_currentUserFromJson(json, session) {
|
|
422
512
|
if (json === null)
|
|
423
513
|
return null;
|
|
424
514
|
const app = this;
|
|
425
515
|
const currentUser = {
|
|
426
516
|
...this._userFromJson(json),
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
517
|
+
_internalSession: session,
|
|
518
|
+
currentSession: {
|
|
519
|
+
async getTokens() {
|
|
520
|
+
const tokens = await session.getPotentiallyExpiredTokens();
|
|
521
|
+
return {
|
|
522
|
+
accessToken: tokens?.accessToken.token ?? null,
|
|
523
|
+
refreshToken: tokens?.refreshToken?.token ?? null
|
|
524
|
+
};
|
|
525
|
+
}
|
|
430
526
|
},
|
|
431
527
|
async updateSelectedTeam(team) {
|
|
432
|
-
await app._updateUser({ selectedTeamId: team?.id ?? null },
|
|
528
|
+
await app._updateUser({ selectedTeamId: team?.id ?? null }, session);
|
|
433
529
|
},
|
|
434
530
|
update(update) {
|
|
435
|
-
return app._updateUser(update,
|
|
531
|
+
return app._updateUser(update, session);
|
|
436
532
|
},
|
|
437
533
|
signOut() {
|
|
438
|
-
return app._signOut(
|
|
534
|
+
return app._signOut(session);
|
|
439
535
|
},
|
|
440
536
|
sendVerificationEmail() {
|
|
441
|
-
return app._sendVerificationEmail(
|
|
537
|
+
return app._sendVerificationEmail(session);
|
|
442
538
|
},
|
|
443
539
|
updatePassword(options) {
|
|
444
|
-
return app._updatePassword(options,
|
|
540
|
+
return app._updatePassword(options, session);
|
|
445
541
|
}
|
|
446
542
|
};
|
|
447
543
|
if (this._isInternalProject()) {
|
|
@@ -500,13 +596,12 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
500
596
|
}
|
|
501
597
|
};
|
|
502
598
|
}
|
|
503
|
-
_createAdminInterface(forProjectId,
|
|
599
|
+
_createAdminInterface(forProjectId, session) {
|
|
504
600
|
return new import_stack_shared.StackAdminInterface({
|
|
505
601
|
baseUrl: this._interface.options.baseUrl,
|
|
506
602
|
projectId: forProjectId,
|
|
507
603
|
clientVersion,
|
|
508
|
-
|
|
509
|
-
refreshProjectOwnerTokens: async () => await this._interface.refreshAccessToken(tokenStore)
|
|
604
|
+
projectOwnerSession: session
|
|
510
605
|
});
|
|
511
606
|
}
|
|
512
607
|
get projectId() {
|
|
@@ -515,6 +610,18 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
515
610
|
get urls() {
|
|
516
611
|
return getUrls(this._urlOptions);
|
|
517
612
|
}
|
|
613
|
+
async getCrossOriginHeaders() {
|
|
614
|
+
return {
|
|
615
|
+
"x-stack-auth": JSON.stringify(await this.getCrossOriginTokenObject())
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
async getCrossOriginTokenObject() {
|
|
619
|
+
const user = await this.getUser();
|
|
620
|
+
if (!user)
|
|
621
|
+
return { accessToken: null, refreshToken: null };
|
|
622
|
+
const tokens = await user.currentSession.getTokens();
|
|
623
|
+
return tokens;
|
|
624
|
+
}
|
|
518
625
|
async _redirectTo(handlerName, options) {
|
|
519
626
|
const url = this.urls[handlerName];
|
|
520
627
|
if (!url) {
|
|
@@ -592,8 +699,8 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
592
699
|
}
|
|
593
700
|
async getUser(options) {
|
|
594
701
|
this._ensurePersistentTokenStore(options?.tokenStore);
|
|
595
|
-
const
|
|
596
|
-
const userJson = await this._currentUserCache.getOrWait([
|
|
702
|
+
const session = this._getSession(options?.tokenStore);
|
|
703
|
+
const userJson = await this._currentUserCache.getOrWait([session], "write-only");
|
|
597
704
|
if (userJson === null) {
|
|
598
705
|
switch (options?.or) {
|
|
599
706
|
case "redirect": {
|
|
@@ -608,20 +715,18 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
608
715
|
}
|
|
609
716
|
}
|
|
610
717
|
}
|
|
611
|
-
return this._currentUserFromJson(userJson,
|
|
718
|
+
return this._currentUserFromJson(userJson, session);
|
|
612
719
|
}
|
|
613
720
|
useUser(options) {
|
|
614
721
|
this._ensurePersistentTokenStore(options?.tokenStore);
|
|
615
722
|
const router = NextNavigation.useRouter();
|
|
616
|
-
const
|
|
617
|
-
const userJson =
|
|
723
|
+
const session = this._getSession(options?.tokenStore);
|
|
724
|
+
const userJson = useAsyncCache(this._currentUserCache, [session], "useUser()");
|
|
725
|
+
const triggerRedirectToSignIn = (0, import_use_trigger.useTrigger)(() => router.replace(this.urls.signIn));
|
|
618
726
|
if (userJson === null) {
|
|
619
727
|
switch (options?.or) {
|
|
620
728
|
case "redirect": {
|
|
621
|
-
(
|
|
622
|
-
await (0, import_promises.wait)(0);
|
|
623
|
-
router.replace(this.urls.signIn);
|
|
624
|
-
});
|
|
729
|
+
triggerRedirectToSignIn();
|
|
625
730
|
(0, import_react3.suspend)();
|
|
626
731
|
throw new import_errors.StackAssertionError("suspend should never return");
|
|
627
732
|
}
|
|
@@ -634,19 +739,19 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
634
739
|
}
|
|
635
740
|
}
|
|
636
741
|
return (0, import_react.useMemo)(() => {
|
|
637
|
-
return this._currentUserFromJson(userJson,
|
|
638
|
-
}, [userJson,
|
|
742
|
+
return this._currentUserFromJson(userJson, session);
|
|
743
|
+
}, [userJson, session, options?.or]);
|
|
639
744
|
}
|
|
640
745
|
onUserChange(callback) {
|
|
641
746
|
this._ensurePersistentTokenStore();
|
|
642
|
-
const
|
|
643
|
-
return this._currentUserCache.onChange([
|
|
644
|
-
callback(this._currentUserFromJson(userJson,
|
|
747
|
+
const session = this._getSession();
|
|
748
|
+
return this._currentUserCache.onChange([session], (userJson) => {
|
|
749
|
+
callback(this._currentUserFromJson(userJson, session));
|
|
645
750
|
});
|
|
646
751
|
}
|
|
647
|
-
async _updateUser(update,
|
|
648
|
-
const res = await this._interface.setClientUserCustomizableData(update,
|
|
649
|
-
await this._refreshUser(
|
|
752
|
+
async _updateUser(update, session) {
|
|
753
|
+
const res = await this._interface.setClientUserCustomizableData(update, session);
|
|
754
|
+
await this._refreshUser(session);
|
|
650
755
|
return res;
|
|
651
756
|
}
|
|
652
757
|
async signInWithOAuth(provider) {
|
|
@@ -655,35 +760,38 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
655
760
|
}
|
|
656
761
|
async signInWithCredential(options) {
|
|
657
762
|
this._ensurePersistentTokenStore();
|
|
658
|
-
const
|
|
659
|
-
const
|
|
660
|
-
if (!
|
|
661
|
-
await this.
|
|
763
|
+
const session = this._getSession();
|
|
764
|
+
const result = await this._interface.signInWithCredential(options.email, options.password, session);
|
|
765
|
+
if (!(result instanceof import_stack_shared.KnownError)) {
|
|
766
|
+
await this._signInToAccountWithTokens(result);
|
|
767
|
+
return await this.redirectToAfterSignIn({ replace: true });
|
|
662
768
|
}
|
|
663
|
-
return
|
|
769
|
+
return result;
|
|
664
770
|
}
|
|
665
771
|
async signUpWithCredential(options) {
|
|
666
772
|
this._ensurePersistentTokenStore();
|
|
667
|
-
const
|
|
773
|
+
const session = this._getSession();
|
|
668
774
|
const emailVerificationRedirectUrl = (0, import_url.constructRedirectUrl)(this.urls.emailVerification);
|
|
669
|
-
const
|
|
775
|
+
const result = await this._interface.signUpWithCredential(
|
|
670
776
|
options.email,
|
|
671
777
|
options.password,
|
|
672
778
|
emailVerificationRedirectUrl,
|
|
673
|
-
|
|
779
|
+
session
|
|
674
780
|
);
|
|
675
|
-
if (!
|
|
676
|
-
await this.
|
|
781
|
+
if (!(result instanceof import_stack_shared.KnownError)) {
|
|
782
|
+
await this._signInToAccountWithTokens(result);
|
|
783
|
+
return await this.redirectToAfterSignUp({ replace: true });
|
|
677
784
|
}
|
|
678
|
-
return
|
|
785
|
+
return result;
|
|
679
786
|
}
|
|
680
787
|
async signInWithMagicLink(code) {
|
|
681
788
|
this._ensurePersistentTokenStore();
|
|
682
|
-
const
|
|
683
|
-
const result = await this._interface.signInWithMagicLink(code,
|
|
789
|
+
const session = this._getSession();
|
|
790
|
+
const result = await this._interface.signInWithMagicLink(code, session);
|
|
684
791
|
if (result instanceof import_stack_shared.KnownError) {
|
|
685
792
|
return result;
|
|
686
793
|
}
|
|
794
|
+
await this._signInToAccountWithTokens(result);
|
|
687
795
|
if (result.newUser) {
|
|
688
796
|
await this.redirectToAfterSignUp({ replace: true });
|
|
689
797
|
} else {
|
|
@@ -692,9 +800,9 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
692
800
|
}
|
|
693
801
|
async callOAuthCallback() {
|
|
694
802
|
this._ensurePersistentTokenStore();
|
|
695
|
-
const
|
|
696
|
-
const result = await (0, import_auth.callOAuthCallback)(this._interface, tokenStore, this.urls.oauthCallback);
|
|
803
|
+
const result = await (0, import_auth.callOAuthCallback)(this._interface, this.urls.oauthCallback);
|
|
697
804
|
if (result) {
|
|
805
|
+
await this._signInToAccountWithTokens(result);
|
|
698
806
|
if (result.newUser) {
|
|
699
807
|
await this.redirectToAfterSignUp({ replace: true });
|
|
700
808
|
return true;
|
|
@@ -705,16 +813,16 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
705
813
|
}
|
|
706
814
|
return false;
|
|
707
815
|
}
|
|
708
|
-
async _signOut(
|
|
709
|
-
await this._interface.signOut(
|
|
816
|
+
async _signOut(session) {
|
|
817
|
+
await this._interface.signOut(session);
|
|
710
818
|
await this.redirectToAfterSignOut();
|
|
711
819
|
}
|
|
712
|
-
async _sendVerificationEmail(
|
|
820
|
+
async _sendVerificationEmail(session) {
|
|
713
821
|
const emailVerificationRedirectUrl = (0, import_url.constructRedirectUrl)(this.urls.emailVerification);
|
|
714
|
-
return await this._interface.sendVerificationEmail(emailVerificationRedirectUrl,
|
|
822
|
+
return await this._interface.sendVerificationEmail(emailVerificationRedirectUrl, session);
|
|
715
823
|
}
|
|
716
|
-
async _updatePassword(options,
|
|
717
|
-
return await this._interface.updatePassword(options,
|
|
824
|
+
async _updatePassword(options, session) {
|
|
825
|
+
return await this._interface.updatePassword(options, session);
|
|
718
826
|
}
|
|
719
827
|
async signOut() {
|
|
720
828
|
const user = await this.getUser();
|
|
@@ -726,64 +834,64 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
726
834
|
return await this._currentProjectCache.getOrWait([], "write-only");
|
|
727
835
|
}
|
|
728
836
|
useProject() {
|
|
729
|
-
return
|
|
837
|
+
return useAsyncCache(this._currentProjectCache, [], "useProject()");
|
|
730
838
|
}
|
|
731
839
|
onProjectChange(callback) {
|
|
732
840
|
return this._currentProjectCache.onChange([], callback);
|
|
733
841
|
}
|
|
734
842
|
async _listOwnedProjects() {
|
|
735
843
|
this._ensureInternalProject();
|
|
736
|
-
const
|
|
737
|
-
const json = await this._ownedProjectsCache.getOrWait([
|
|
844
|
+
const session = this._getSession();
|
|
845
|
+
const json = await this._ownedProjectsCache.getOrWait([session], "write-only");
|
|
738
846
|
return json.map((j) => this._projectAdminFromJson(
|
|
739
847
|
j,
|
|
740
|
-
this._createAdminInterface(j.id,
|
|
741
|
-
() => this._refreshOwnedProjects(
|
|
848
|
+
this._createAdminInterface(j.id, session),
|
|
849
|
+
() => this._refreshOwnedProjects(session)
|
|
742
850
|
));
|
|
743
851
|
}
|
|
744
852
|
_useOwnedProjects() {
|
|
745
853
|
this._ensureInternalProject();
|
|
746
|
-
const
|
|
747
|
-
const json =
|
|
854
|
+
const session = this._getSession();
|
|
855
|
+
const json = useAsyncCache(this._ownedProjectsCache, [session], "useOwnedProjects()");
|
|
748
856
|
return (0, import_react.useMemo)(() => json.map((j) => this._projectAdminFromJson(
|
|
749
857
|
j,
|
|
750
|
-
this._createAdminInterface(j.id,
|
|
751
|
-
() => this._refreshOwnedProjects(
|
|
858
|
+
this._createAdminInterface(j.id, session),
|
|
859
|
+
() => this._refreshOwnedProjects(session)
|
|
752
860
|
)), [json]);
|
|
753
861
|
}
|
|
754
862
|
_onOwnedProjectsChange(callback) {
|
|
755
863
|
this._ensureInternalProject();
|
|
756
|
-
const
|
|
757
|
-
return this._ownedProjectsCache.onChange([
|
|
864
|
+
const session = this._getSession();
|
|
865
|
+
return this._ownedProjectsCache.onChange([session], (projects) => {
|
|
758
866
|
callback(projects.map((j) => this._projectAdminFromJson(
|
|
759
867
|
j,
|
|
760
|
-
this._createAdminInterface(j.id,
|
|
761
|
-
() => this._refreshOwnedProjects(
|
|
868
|
+
this._createAdminInterface(j.id, session),
|
|
869
|
+
() => this._refreshOwnedProjects(session)
|
|
762
870
|
)));
|
|
763
871
|
});
|
|
764
872
|
}
|
|
765
873
|
async _createProject(newProject) {
|
|
766
874
|
this._ensureInternalProject();
|
|
767
|
-
const
|
|
768
|
-
const json = await this._interface.createProject(newProject,
|
|
875
|
+
const session = this._getSession();
|
|
876
|
+
const json = await this._interface.createProject(newProject, session);
|
|
769
877
|
const res = this._projectAdminFromJson(
|
|
770
878
|
json,
|
|
771
|
-
this._createAdminInterface(json.id,
|
|
772
|
-
() => this._refreshOwnedProjects(
|
|
879
|
+
this._createAdminInterface(json.id, session),
|
|
880
|
+
() => this._refreshOwnedProjects(session)
|
|
773
881
|
);
|
|
774
|
-
await this._refreshOwnedProjects(
|
|
882
|
+
await this._refreshOwnedProjects(session);
|
|
775
883
|
return res;
|
|
776
884
|
}
|
|
777
|
-
async _refreshUser(
|
|
778
|
-
await this._currentUserCache.refresh([
|
|
885
|
+
async _refreshUser(session) {
|
|
886
|
+
await this._currentUserCache.refresh([session]);
|
|
779
887
|
}
|
|
780
888
|
async _refreshUsers() {
|
|
781
889
|
}
|
|
782
890
|
async _refreshProject() {
|
|
783
891
|
await this._currentProjectCache.refresh([]);
|
|
784
892
|
}
|
|
785
|
-
async _refreshOwnedProjects(
|
|
786
|
-
await this._ownedProjectsCache.refresh([
|
|
893
|
+
async _refreshOwnedProjects(session) {
|
|
894
|
+
await this._ownedProjectsCache.refresh([session]);
|
|
787
895
|
}
|
|
788
896
|
static get [stackAppInternalsSymbol]() {
|
|
789
897
|
return {
|
|
@@ -822,15 +930,15 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
822
930
|
};
|
|
823
931
|
},
|
|
824
932
|
setCurrentUser: (userJsonPromise) => {
|
|
825
|
-
(0, import_promises.runAsynchronously)(this._currentUserCache.forceSetCachedValueAsync([this.
|
|
933
|
+
(0, import_promises.runAsynchronously)(this._currentUserCache.forceSetCachedValueAsync([this._getSession()], userJsonPromise));
|
|
826
934
|
}
|
|
827
935
|
};
|
|
828
936
|
}
|
|
829
937
|
};
|
|
830
938
|
var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
831
939
|
// TODO override the client user cache to use the server user cache, so we save some requests
|
|
832
|
-
_currentServerUserCache =
|
|
833
|
-
const user = await this._interface.getServerUserByToken(
|
|
940
|
+
_currentServerUserCache = createCacheBySession(async (session) => {
|
|
941
|
+
const user = await this._interface.getServerUserByToken(session);
|
|
834
942
|
return import_results.Result.or(user, null);
|
|
835
943
|
});
|
|
836
944
|
_serverUsersCache = createCache(async () => {
|
|
@@ -921,15 +1029,15 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
921
1029
|
});
|
|
922
1030
|
},
|
|
923
1031
|
async listTeams() {
|
|
924
|
-
const teams = await app._serverTeamsCache.getOrWait([app.
|
|
1032
|
+
const teams = await app._serverTeamsCache.getOrWait([app._getSession()], "write-only");
|
|
925
1033
|
return teams.map((json2) => app._serverTeamFromJson(json2));
|
|
926
1034
|
},
|
|
927
1035
|
useTeams() {
|
|
928
|
-
const teams =
|
|
1036
|
+
const teams = useAsyncCache(app._serverTeamsCache, [app._getSession()], "user.useTeams()");
|
|
929
1037
|
return (0, import_react.useMemo)(() => teams.map((json2) => app._serverTeamFromJson(json2)), [teams]);
|
|
930
1038
|
},
|
|
931
1039
|
onTeamsChange(callback) {
|
|
932
|
-
return app._serverTeamsCache.onChange([app.
|
|
1040
|
+
return app._serverTeamsCache.onChange([app._getSession()], (value, oldValue) => {
|
|
933
1041
|
callback(value.map((json2) => app._serverTeamFromJson(json2)), oldValue?.map((json2) => app._serverTeamFromJson(json2)));
|
|
934
1042
|
});
|
|
935
1043
|
},
|
|
@@ -938,7 +1046,7 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
938
1046
|
return permissions.map((json2) => app._serverPermissionFromJson(json2));
|
|
939
1047
|
},
|
|
940
1048
|
usePermissions(scope, options) {
|
|
941
|
-
const permissions =
|
|
1049
|
+
const permissions = useAsyncCache(app._serverTeamUserPermissionsCache, [scope.id, json.id, "team", !!options?.direct], "user.usePermissions()");
|
|
942
1050
|
return (0, import_react.useMemo)(() => permissions.map((json2) => app._serverPermissionFromJson(json2)), [permissions]);
|
|
943
1051
|
},
|
|
944
1052
|
usePermission(scope, permissionId) {
|
|
@@ -958,20 +1066,26 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
958
1066
|
}
|
|
959
1067
|
};
|
|
960
1068
|
}
|
|
961
|
-
_currentServerUserFromJson(json,
|
|
1069
|
+
_currentServerUserFromJson(json, session) {
|
|
962
1070
|
if (json === null)
|
|
963
1071
|
return null;
|
|
964
1072
|
const app = this;
|
|
965
1073
|
const nonCurrentServerUser = this._serverUserFromJson(json);
|
|
966
1074
|
const currentUser = {
|
|
967
1075
|
...nonCurrentServerUser,
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
1076
|
+
_internalSession: session,
|
|
1077
|
+
currentSession: {
|
|
1078
|
+
async getTokens() {
|
|
1079
|
+
const tokens = await session.getPotentiallyExpiredTokens();
|
|
1080
|
+
return {
|
|
1081
|
+
accessToken: tokens?.accessToken.token ?? null,
|
|
1082
|
+
refreshToken: tokens?.refreshToken?.token ?? null
|
|
1083
|
+
};
|
|
1084
|
+
}
|
|
971
1085
|
},
|
|
972
1086
|
async delete() {
|
|
973
1087
|
const res = await nonCurrentServerUser.delete();
|
|
974
|
-
await app._refreshUser(
|
|
1088
|
+
await app._refreshUser(session);
|
|
975
1089
|
return res;
|
|
976
1090
|
},
|
|
977
1091
|
async updateSelectedTeam(team) {
|
|
@@ -979,20 +1093,20 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
979
1093
|
},
|
|
980
1094
|
async update(update) {
|
|
981
1095
|
const res = await nonCurrentServerUser.update(update);
|
|
982
|
-
await app._refreshUser(
|
|
1096
|
+
await app._refreshUser(session);
|
|
983
1097
|
return res;
|
|
984
1098
|
},
|
|
985
1099
|
signOut() {
|
|
986
|
-
return app._signOut(
|
|
1100
|
+
return app._signOut(session);
|
|
987
1101
|
},
|
|
988
1102
|
getClientUser() {
|
|
989
|
-
return app._currentUserFromJson(json,
|
|
1103
|
+
return app._currentUserFromJson(json, session);
|
|
990
1104
|
},
|
|
991
1105
|
sendVerificationEmail() {
|
|
992
|
-
return app._sendVerificationEmail(
|
|
1106
|
+
return app._sendVerificationEmail(session);
|
|
993
1107
|
},
|
|
994
1108
|
updatePassword(options) {
|
|
995
|
-
return app._updatePassword(options,
|
|
1109
|
+
return app._updatePassword(options, session);
|
|
996
1110
|
}
|
|
997
1111
|
};
|
|
998
1112
|
if (this._isInternalProject()) {
|
|
@@ -1050,7 +1164,7 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1050
1164
|
await app._serverTeamsCache.refresh([]);
|
|
1051
1165
|
},
|
|
1052
1166
|
useMembers() {
|
|
1053
|
-
const result =
|
|
1167
|
+
const result = useAsyncCache(app._serverTeamMembersCache, [json.id], "team.useUsers()");
|
|
1054
1168
|
return (0, import_react.useMemo)(() => result.map((u) => app._serverTeamMemberFromJson(u)), [result]);
|
|
1055
1169
|
},
|
|
1056
1170
|
async addUser(userId) {
|
|
@@ -1074,9 +1188,9 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1074
1188
|
}
|
|
1075
1189
|
async getServerUser() {
|
|
1076
1190
|
this._ensurePersistentTokenStore();
|
|
1077
|
-
const
|
|
1078
|
-
const userJson = await this._currentServerUserCache.getOrWait([
|
|
1079
|
-
return this._currentServerUserFromJson(userJson,
|
|
1191
|
+
const session = this._getSession();
|
|
1192
|
+
const userJson = await this._currentServerUserCache.getOrWait([session], "write-only");
|
|
1193
|
+
return this._currentServerUserFromJson(userJson, session);
|
|
1080
1194
|
}
|
|
1081
1195
|
async getServerUserById(userId) {
|
|
1082
1196
|
const json = await this._serverUserCache.getOrWait([userId], "write-only");
|
|
@@ -1084,20 +1198,20 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1084
1198
|
}
|
|
1085
1199
|
useServerUser(options) {
|
|
1086
1200
|
this._ensurePersistentTokenStore();
|
|
1087
|
-
const
|
|
1088
|
-
const userJson =
|
|
1201
|
+
const session = this._getSession();
|
|
1202
|
+
const userJson = useAsyncCache(this._currentServerUserCache, [session], "useServerUser()");
|
|
1089
1203
|
return (0, import_react.useMemo)(() => {
|
|
1090
1204
|
if (options?.required && userJson === null) {
|
|
1091
1205
|
(0, import_react.use)(this.redirectToSignIn());
|
|
1092
1206
|
}
|
|
1093
|
-
return this._currentServerUserFromJson(userJson,
|
|
1094
|
-
}, [userJson,
|
|
1207
|
+
return this._currentServerUserFromJson(userJson, session);
|
|
1208
|
+
}, [userJson, session, options?.required]);
|
|
1095
1209
|
}
|
|
1096
1210
|
onServerUserChange(callback) {
|
|
1097
1211
|
this._ensurePersistentTokenStore();
|
|
1098
|
-
const
|
|
1099
|
-
return this._currentServerUserCache.onChange([
|
|
1100
|
-
callback(this._currentServerUserFromJson(userJson,
|
|
1212
|
+
const session = this._getSession();
|
|
1213
|
+
return this._currentServerUserCache.onChange([session], (userJson) => {
|
|
1214
|
+
callback(this._currentServerUserFromJson(userJson, session));
|
|
1101
1215
|
});
|
|
1102
1216
|
}
|
|
1103
1217
|
async listServerUsers() {
|
|
@@ -1105,7 +1219,7 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1105
1219
|
return json.map((j) => this._serverUserFromJson(j));
|
|
1106
1220
|
}
|
|
1107
1221
|
useServerUsers() {
|
|
1108
|
-
const json =
|
|
1222
|
+
const json = useAsyncCache(this._serverUsersCache, [], "useServerUsers()");
|
|
1109
1223
|
return (0, import_react.useMemo)(() => {
|
|
1110
1224
|
return json.map((j) => this._serverUserFromJson(j));
|
|
1111
1225
|
}, [json]);
|
|
@@ -1119,7 +1233,7 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1119
1233
|
return await this._serverTeamPermissionDefinitionsCache.getOrWait([], "write-only");
|
|
1120
1234
|
}
|
|
1121
1235
|
usePermissionDefinitions() {
|
|
1122
|
-
return
|
|
1236
|
+
return useAsyncCache(this._serverTeamPermissionDefinitionsCache, [], "usePermissions()");
|
|
1123
1237
|
}
|
|
1124
1238
|
_serverPermissionFromJson(json) {
|
|
1125
1239
|
return {
|
|
@@ -1130,7 +1244,7 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1130
1244
|
};
|
|
1131
1245
|
}
|
|
1132
1246
|
async createPermissionDefinition(data) {
|
|
1133
|
-
const permission =
|
|
1247
|
+
const permission = this._serverPermissionFromJson(await this._interface.createPermissionDefinition(data));
|
|
1134
1248
|
await this._serverTeamPermissionDefinitionsCache.refresh([]);
|
|
1135
1249
|
return permission;
|
|
1136
1250
|
}
|
|
@@ -1152,7 +1266,7 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1152
1266
|
return this._serverTeamFromJson(team);
|
|
1153
1267
|
}
|
|
1154
1268
|
useTeams() {
|
|
1155
|
-
const teams =
|
|
1269
|
+
const teams = useAsyncCache(this._serverTeamsCache, [], "useServerTeams()");
|
|
1156
1270
|
return (0, import_react.useMemo)(() => {
|
|
1157
1271
|
return teams.map((t) => this._serverTeamFromJson(t));
|
|
1158
1272
|
}, [teams]);
|
|
@@ -1167,10 +1281,10 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1167
1281
|
return teams.find((t) => t.id === teamId) ?? null;
|
|
1168
1282
|
}, [teams, teamId]);
|
|
1169
1283
|
}
|
|
1170
|
-
async _refreshUser(
|
|
1284
|
+
async _refreshUser(session) {
|
|
1171
1285
|
await Promise.all([
|
|
1172
|
-
super._refreshUser(
|
|
1173
|
-
this._currentServerUserCache.refresh([
|
|
1286
|
+
super._refreshUser(session),
|
|
1287
|
+
this._currentServerUserCache.refresh([session])
|
|
1174
1288
|
]);
|
|
1175
1289
|
}
|
|
1176
1290
|
async _refreshUsers() {
|
|
@@ -1180,7 +1294,7 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1180
1294
|
]);
|
|
1181
1295
|
}
|
|
1182
1296
|
useEmailTemplates() {
|
|
1183
|
-
return
|
|
1297
|
+
return useAsyncCache(this._serverEmailTemplatesCache, [], "useEmailTemplates()");
|
|
1184
1298
|
}
|
|
1185
1299
|
async listEmailTemplates() {
|
|
1186
1300
|
return await this._serverEmailTemplatesCache.getOrWait([], "write-only");
|
|
@@ -1207,9 +1321,8 @@ var _StackAdminAppImpl = class extends _StackServerAppImpl {
|
|
|
1207
1321
|
baseUrl: options.baseUrl ?? getDefaultBaseUrl(),
|
|
1208
1322
|
projectId: options.projectId ?? getDefaultProjectId(),
|
|
1209
1323
|
clientVersion,
|
|
1210
|
-
..."
|
|
1211
|
-
|
|
1212
|
-
refreshProjectOwnerTokens: options.refreshProjectOwnerTokens
|
|
1324
|
+
..."projectOwnerSession" in options ? {
|
|
1325
|
+
projectOwnerSession: options.projectOwnerSession
|
|
1213
1326
|
} : {
|
|
1214
1327
|
publishableClientKey: options.publishableClientKey ?? getDefaultPublishableClientKey(),
|
|
1215
1328
|
secretServerKey: options.secretServerKey ?? getDefaultSecretServerKey(),
|
|
@@ -1269,7 +1382,7 @@ var _StackAdminAppImpl = class extends _StackServerAppImpl {
|
|
|
1269
1382
|
);
|
|
1270
1383
|
}
|
|
1271
1384
|
useProjectAdmin() {
|
|
1272
|
-
const json =
|
|
1385
|
+
const json = useAsyncCache(this._adminProjectCache, [], "useProjectAdmin()");
|
|
1273
1386
|
return (0, import_react.useMemo)(() => this._projectAdminFromJson(
|
|
1274
1387
|
json,
|
|
1275
1388
|
this._interface,
|
|
@@ -1290,7 +1403,7 @@ var _StackAdminAppImpl = class extends _StackServerAppImpl {
|
|
|
1290
1403
|
return json.map((j) => this._createApiKeySetFromJson(j));
|
|
1291
1404
|
}
|
|
1292
1405
|
useApiKeySets() {
|
|
1293
|
-
const json =
|
|
1406
|
+
const json = useAsyncCache(this._apiKeySetsCache, [], "useApiKeySets()");
|
|
1294
1407
|
return (0, import_react.useMemo)(() => {
|
|
1295
1408
|
return json.map((j) => this._createApiKeySetFromJson(j));
|
|
1296
1409
|
}, [json]);
|