@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
|
@@ -6,21 +6,23 @@ import { StackAssertionError, throwErr } from "@stackframe/stack-shared/dist/uti
|
|
|
6
6
|
import { generateUuid } from "@stackframe/stack-shared/dist/utils/uuids";
|
|
7
7
|
import { AsyncResult, Result } from "@stackframe/stack-shared/dist/utils/results";
|
|
8
8
|
import { suspendIfSsr } from "@stackframe/stack-shared/dist/utils/react";
|
|
9
|
-
import {
|
|
9
|
+
import { Store } from "@stackframe/stack-shared/dist/utils/stores";
|
|
10
10
|
import { getProductionModeErrors } from "@stackframe/stack-shared/dist/interface/clientInterface";
|
|
11
|
-
import {
|
|
11
|
+
import { isBrowserLike } from "@stackframe/stack-shared/src/utils/env";
|
|
12
12
|
import { callOAuthCallback, signInWithOAuth } from "./auth";
|
|
13
13
|
import * as NextNavigationUnscrambled from "next/navigation";
|
|
14
14
|
import { constructRedirectUrl } from "../utils/url";
|
|
15
|
-
import { filterUndefined, omit } from "@stackframe/stack-shared/dist/utils/objects";
|
|
15
|
+
import { deepPlainEquals, filterUndefined, omit } from "@stackframe/stack-shared/dist/utils/objects";
|
|
16
16
|
import { resolved, runAsynchronously, wait } from "@stackframe/stack-shared/dist/utils/promises";
|
|
17
17
|
import { AsyncCache } from "@stackframe/stack-shared/dist/utils/caches";
|
|
18
18
|
import { suspend } from "@stackframe/stack-shared/dist/utils/react";
|
|
19
19
|
import { scrambleDuringCompileTime } from "@stackframe/stack-shared/dist/utils/compile-time";
|
|
20
20
|
import { isReactServer } from "@stackframe/stack-sc";
|
|
21
21
|
import * as cookie from "cookie";
|
|
22
|
+
import { InternalSession } from "@stackframe/stack-shared/dist/sessions";
|
|
23
|
+
import { useTrigger } from "@stackframe/stack-shared/dist/hooks/use-trigger";
|
|
22
24
|
var NextNavigation = scrambleDuringCompileTime(NextNavigationUnscrambled);
|
|
23
|
-
var clientVersion = "js @stackframe/stack@2.4.
|
|
25
|
+
var clientVersion = "js @stackframe/stack@2.4.20";
|
|
24
26
|
function permissionDefinitionScopeToType(scope) {
|
|
25
27
|
return { "any-team": "team", "specific-team": "team", "global": "global" }[scope.type];
|
|
26
28
|
}
|
|
@@ -61,45 +63,13 @@ function getDefaultBaseUrl() {
|
|
|
61
63
|
}
|
|
62
64
|
var defaultBaseUrl = "https://app.stack-auth.com";
|
|
63
65
|
function createEmptyTokenStore() {
|
|
64
|
-
return new
|
|
66
|
+
return new Store({
|
|
65
67
|
refreshToken: null,
|
|
66
68
|
accessToken: null
|
|
67
69
|
});
|
|
68
70
|
}
|
|
69
|
-
var cookieTokenStore = null;
|
|
70
|
-
var cookieTokenStoreInitializer = () => {
|
|
71
|
-
if (!isClient()) {
|
|
72
|
-
throw new Error("Cannot use cookie token store on the server!");
|
|
73
|
-
}
|
|
74
|
-
if (cookieTokenStore === null) {
|
|
75
|
-
cookieTokenStore = new AsyncStore();
|
|
76
|
-
let hasSucceededInWriting = true;
|
|
77
|
-
setInterval(() => {
|
|
78
|
-
if (hasSucceededInWriting) {
|
|
79
|
-
const newValue = {
|
|
80
|
-
refreshToken: getCookie("stack-refresh"),
|
|
81
|
-
accessToken: getCookie("stack-access")
|
|
82
|
-
};
|
|
83
|
-
const res = cookieTokenStore.get();
|
|
84
|
-
if (res.status !== "ok" || res.data.refreshToken !== newValue.refreshToken || res.data.accessToken !== newValue.accessToken) {
|
|
85
|
-
cookieTokenStore.set(newValue);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}, 10);
|
|
89
|
-
cookieTokenStore.onChange((value) => {
|
|
90
|
-
try {
|
|
91
|
-
setOrDeleteCookie("stack-refresh", value.refreshToken, { maxAge: 60 * 60 * 24 * 365 });
|
|
92
|
-
setOrDeleteCookie("stack-access", value.accessToken, { maxAge: 60 * 60 * 24 });
|
|
93
|
-
hasSucceededInWriting = true;
|
|
94
|
-
} catch (e) {
|
|
95
|
-
hasSucceededInWriting = false;
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
return cookieTokenStore;
|
|
100
|
-
};
|
|
101
71
|
var loadingSentinel = Symbol("stackAppCacheLoadingSentinel");
|
|
102
|
-
function
|
|
72
|
+
function useAsyncCache(cache, dependencies, caller) {
|
|
103
73
|
suspendIfSsr(caller);
|
|
104
74
|
const subscribe = useCallback((cb) => {
|
|
105
75
|
const { unsubscribe } = cache.onChange(dependencies, () => cb());
|
|
@@ -123,17 +93,13 @@ var createCache = (fetcher) => {
|
|
|
123
93
|
{}
|
|
124
94
|
);
|
|
125
95
|
};
|
|
126
|
-
var
|
|
96
|
+
var createCacheBySession = (fetcher) => {
|
|
127
97
|
return new AsyncCache(
|
|
128
|
-
async ([
|
|
98
|
+
async ([session, ...extraDependencies]) => await fetcher(session, extraDependencies),
|
|
129
99
|
{
|
|
130
|
-
onSubscribe: ([
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
return;
|
|
134
|
-
refresh();
|
|
135
|
-
});
|
|
136
|
-
return () => handlerObj.unsubscribe();
|
|
100
|
+
onSubscribe: ([session], refresh) => {
|
|
101
|
+
const handler = session.onInvalidate(() => refresh());
|
|
102
|
+
return () => handler.unsubscribe();
|
|
137
103
|
}
|
|
138
104
|
}
|
|
139
105
|
);
|
|
@@ -160,7 +126,7 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
160
126
|
}
|
|
161
127
|
numberOfAppsCreated++;
|
|
162
128
|
if (numberOfAppsCreated > 10) {
|
|
163
|
-
console.warn(`You have created more than 10 Stack apps (${numberOfAppsCreated}). This is usually a sign of a memory leak.
|
|
129
|
+
(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).`);
|
|
164
130
|
}
|
|
165
131
|
}
|
|
166
132
|
_uniqueIdentifier = void 0;
|
|
@@ -168,24 +134,24 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
168
134
|
_tokenStoreInit;
|
|
169
135
|
_urlOptions;
|
|
170
136
|
__DEMO_ENABLE_SLIGHT_FETCH_DELAY = false;
|
|
171
|
-
_currentUserCache =
|
|
137
|
+
_currentUserCache = createCacheBySession(async (session) => {
|
|
172
138
|
if (this.__DEMO_ENABLE_SLIGHT_FETCH_DELAY) {
|
|
173
139
|
await wait(2e3);
|
|
174
140
|
}
|
|
175
|
-
const user = await this._interface.getClientUserByToken(
|
|
141
|
+
const user = await this._interface.getClientUserByToken(session);
|
|
176
142
|
return Result.or(user, null);
|
|
177
143
|
});
|
|
178
144
|
_currentProjectCache = createCache(async () => {
|
|
179
145
|
return Result.orThrow(await this._interface.getClientProject());
|
|
180
146
|
});
|
|
181
|
-
_ownedProjectsCache =
|
|
182
|
-
return await this._interface.listProjects(
|
|
147
|
+
_ownedProjectsCache = createCacheBySession(async (session) => {
|
|
148
|
+
return await this._interface.listProjects(session);
|
|
183
149
|
});
|
|
184
|
-
_currentUserPermissionsCache =
|
|
185
|
-
return await this._interface.listClientUserTeamPermissions({ teamId, type, direct },
|
|
150
|
+
_currentUserPermissionsCache = createCacheBySession(async (session, [teamId, type, direct]) => {
|
|
151
|
+
return await this._interface.listClientUserTeamPermissions({ teamId, type, direct }, session);
|
|
186
152
|
});
|
|
187
|
-
_currentUserTeamsCache =
|
|
188
|
-
return await this._interface.listClientUserTeams(
|
|
153
|
+
_currentUserTeamsCache = createCacheBySession(async (session) => {
|
|
154
|
+
return await this._interface.listClientUserTeams(session);
|
|
189
155
|
});
|
|
190
156
|
_initUniqueIdentifier() {
|
|
191
157
|
if (!this._uniqueIdentifier) {
|
|
@@ -208,26 +174,73 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
208
174
|
return this._uniqueIdentifier;
|
|
209
175
|
}
|
|
210
176
|
_memoryTokenStore = createEmptyTokenStore();
|
|
211
|
-
_requestTokenStores = /* @__PURE__ */ new
|
|
212
|
-
|
|
177
|
+
_requestTokenStores = /* @__PURE__ */ new WeakMap();
|
|
178
|
+
_storedCookieTokenStore = null;
|
|
179
|
+
get _refreshTokenCookieName() {
|
|
180
|
+
return `stack-refresh-${this.projectId}`;
|
|
181
|
+
}
|
|
182
|
+
get _accessTokenCookieName() {
|
|
183
|
+
return `stack-access`;
|
|
184
|
+
}
|
|
185
|
+
_getCookieTokenStore() {
|
|
186
|
+
if (!isBrowserLike()) {
|
|
187
|
+
throw new Error("Cannot use cookie token store on the server!");
|
|
188
|
+
}
|
|
189
|
+
if (this._storedCookieTokenStore === null) {
|
|
190
|
+
const getCurrentValue = (old) => ({
|
|
191
|
+
refreshToken: getCookie(this._refreshTokenCookieName) ?? getCookie("stack-refresh"),
|
|
192
|
+
// keep old cookie name for backwards-compatibility
|
|
193
|
+
// if there is an access token in memory already, don't update the access token based on cookies (access token
|
|
194
|
+
// cookies may be set by another project on the same domain)
|
|
195
|
+
// see the comment in _accessTokenCookieName for more information
|
|
196
|
+
accessToken: old === null ? getCookie(this._accessTokenCookieName) : old.accessToken
|
|
197
|
+
});
|
|
198
|
+
this._storedCookieTokenStore = new Store(getCurrentValue(null));
|
|
199
|
+
let hasSucceededInWriting = true;
|
|
200
|
+
setInterval(() => {
|
|
201
|
+
if (hasSucceededInWriting) {
|
|
202
|
+
const oldValue = this._storedCookieTokenStore.get();
|
|
203
|
+
const currentValue = getCurrentValue(oldValue);
|
|
204
|
+
if (!deepPlainEquals(currentValue, oldValue)) {
|
|
205
|
+
this._storedCookieTokenStore.set(currentValue);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}, 100);
|
|
209
|
+
this._storedCookieTokenStore.onChange((value) => {
|
|
210
|
+
try {
|
|
211
|
+
setOrDeleteCookie(this._refreshTokenCookieName, value.refreshToken, { maxAge: 60 * 60 * 24 * 365 });
|
|
212
|
+
setOrDeleteCookie(this._accessTokenCookieName, value.accessToken, { maxAge: 60 * 60 * 24 });
|
|
213
|
+
hasSucceededInWriting = true;
|
|
214
|
+
} catch (e) {
|
|
215
|
+
if (!isBrowserLike()) {
|
|
216
|
+
hasSucceededInWriting = false;
|
|
217
|
+
} else {
|
|
218
|
+
throw e;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
return this._storedCookieTokenStore;
|
|
224
|
+
}
|
|
225
|
+
_getOrCreateTokenStore(overrideTokenStoreInit) {
|
|
213
226
|
const tokenStoreInit = overrideTokenStoreInit === void 0 ? this._tokenStoreInit : overrideTokenStoreInit;
|
|
214
227
|
switch (tokenStoreInit) {
|
|
215
228
|
case "cookie": {
|
|
216
|
-
return
|
|
229
|
+
return this._getCookieTokenStore();
|
|
217
230
|
}
|
|
218
231
|
case "nextjs-cookie": {
|
|
219
|
-
if (
|
|
220
|
-
return
|
|
232
|
+
if (isBrowserLike()) {
|
|
233
|
+
return this._getCookieTokenStore();
|
|
221
234
|
} else {
|
|
222
|
-
const store = new
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
accessToken: getCookie(
|
|
235
|
+
const store = new Store({
|
|
236
|
+
refreshToken: getCookie(this._refreshTokenCookieName) ?? getCookie("stack-refresh"),
|
|
237
|
+
// keep old cookie name for backwards-compatibility
|
|
238
|
+
accessToken: getCookie(this._accessTokenCookieName)
|
|
226
239
|
});
|
|
227
240
|
store.onChange((value) => {
|
|
228
241
|
try {
|
|
229
|
-
setOrDeleteCookie(
|
|
230
|
-
setOrDeleteCookie(
|
|
242
|
+
setOrDeleteCookie(this._refreshTokenCookieName, value.refreshToken, { maxAge: 60 * 60 * 24 * 365 });
|
|
243
|
+
setOrDeleteCookie(this._accessTokenCookieName, value.accessToken, { maxAge: 60 * 60 * 24 });
|
|
231
244
|
} catch (e) {
|
|
232
245
|
}
|
|
233
246
|
});
|
|
@@ -237,26 +250,102 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
237
250
|
case "memory": {
|
|
238
251
|
return this._memoryTokenStore;
|
|
239
252
|
}
|
|
240
|
-
case null: {
|
|
241
|
-
return createEmptyTokenStore();
|
|
242
|
-
}
|
|
243
253
|
default: {
|
|
244
|
-
if (tokenStoreInit
|
|
254
|
+
if (tokenStoreInit === null) {
|
|
255
|
+
return createEmptyTokenStore();
|
|
256
|
+
} else if (typeof tokenStoreInit === "object" && "headers" in tokenStoreInit) {
|
|
245
257
|
if (this._requestTokenStores.has(tokenStoreInit))
|
|
246
258
|
return this._requestTokenStores.get(tokenStoreInit);
|
|
259
|
+
const stackAuthHeader = tokenStoreInit.headers.get("x-stack-auth");
|
|
260
|
+
if (stackAuthHeader) {
|
|
261
|
+
let parsed2;
|
|
262
|
+
try {
|
|
263
|
+
parsed2 = JSON.parse(stackAuthHeader);
|
|
264
|
+
if (typeof parsed2 !== "object")
|
|
265
|
+
throw new Error("x-stack-auth header must be a JSON object");
|
|
266
|
+
if (parsed2 === null)
|
|
267
|
+
throw new Error("x-stack-auth header must not be null");
|
|
268
|
+
} catch (e) {
|
|
269
|
+
throw new Error(`Invalid x-stack-auth header: ${stackAuthHeader}`, { cause: e });
|
|
270
|
+
}
|
|
271
|
+
return this._getOrCreateTokenStore({
|
|
272
|
+
accessToken: parsed2.accessToken ?? null,
|
|
273
|
+
refreshToken: parsed2.refreshToken ?? null
|
|
274
|
+
});
|
|
275
|
+
}
|
|
247
276
|
const cookieHeader = tokenStoreInit.headers.get("cookie");
|
|
248
277
|
const parsed = cookie.parse(cookieHeader || "");
|
|
249
|
-
const res = new
|
|
250
|
-
refreshToken: parsed["stack-refresh"] || null,
|
|
251
|
-
|
|
278
|
+
const res = new Store({
|
|
279
|
+
refreshToken: parsed[this._refreshTokenCookieName] || parsed["stack-refresh"] || null,
|
|
280
|
+
// keep old cookie name for backwards-compatibility
|
|
281
|
+
accessToken: parsed[this._accessTokenCookieName] || null
|
|
252
282
|
});
|
|
253
283
|
this._requestTokenStores.set(tokenStoreInit, res);
|
|
254
284
|
return res;
|
|
285
|
+
} else if ("accessToken" in tokenStoreInit || "refreshToken" in tokenStoreInit) {
|
|
286
|
+
return new Store({
|
|
287
|
+
refreshToken: tokenStoreInit.refreshToken,
|
|
288
|
+
accessToken: tokenStoreInit.accessToken
|
|
289
|
+
});
|
|
255
290
|
}
|
|
256
291
|
throw new Error(`Invalid token store ${tokenStoreInit}`);
|
|
257
292
|
}
|
|
258
293
|
}
|
|
259
294
|
}
|
|
295
|
+
/**
|
|
296
|
+
* A map from token stores and session keys to sessions.
|
|
297
|
+
*
|
|
298
|
+
* This isn't just a map from session keys to sessions for two reasons:
|
|
299
|
+
*
|
|
300
|
+
* - So we can garbage-collect Session objects when the token store is garbage-collected
|
|
301
|
+
* - 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
|
|
302
|
+
*/
|
|
303
|
+
_sessionsByTokenStoreAndSessionKey = /* @__PURE__ */ new WeakMap();
|
|
304
|
+
_getSessionFromTokenStore(tokenStore) {
|
|
305
|
+
const tokenObj = tokenStore.get();
|
|
306
|
+
const sessionKey = InternalSession.calculateSessionKey(tokenObj);
|
|
307
|
+
const existing = sessionKey ? this._sessionsByTokenStoreAndSessionKey.get(tokenStore)?.get(sessionKey) : null;
|
|
308
|
+
if (existing)
|
|
309
|
+
return existing;
|
|
310
|
+
const session = this._interface.createSession({
|
|
311
|
+
refreshToken: tokenObj.refreshToken,
|
|
312
|
+
accessToken: tokenObj.accessToken
|
|
313
|
+
});
|
|
314
|
+
session.onAccessTokenChange((newAccessToken) => {
|
|
315
|
+
tokenStore.update((old) => ({
|
|
316
|
+
...old,
|
|
317
|
+
accessToken: newAccessToken?.token ?? null
|
|
318
|
+
}));
|
|
319
|
+
});
|
|
320
|
+
session.onInvalidate(() => {
|
|
321
|
+
tokenStore.update((old) => ({
|
|
322
|
+
...old,
|
|
323
|
+
accessToken: null,
|
|
324
|
+
refreshToken: null
|
|
325
|
+
}));
|
|
326
|
+
});
|
|
327
|
+
let sessionsBySessionKey = this._sessionsByTokenStoreAndSessionKey.get(tokenStore) ?? /* @__PURE__ */ new Map();
|
|
328
|
+
this._sessionsByTokenStoreAndSessionKey.set(tokenStore, sessionsBySessionKey);
|
|
329
|
+
sessionsBySessionKey.set(sessionKey, session);
|
|
330
|
+
return session;
|
|
331
|
+
}
|
|
332
|
+
_getSession(overrideTokenStoreInit) {
|
|
333
|
+
const tokenStore = this._getOrCreateTokenStore(overrideTokenStoreInit);
|
|
334
|
+
return this._getSessionFromTokenStore(tokenStore);
|
|
335
|
+
}
|
|
336
|
+
_useSession(overrideTokenStoreInit) {
|
|
337
|
+
const tokenStore = this._getOrCreateTokenStore(overrideTokenStoreInit);
|
|
338
|
+
const subscribe = useCallback((cb) => {
|
|
339
|
+
const { unsubscribe } = tokenStore.onChange(() => cb());
|
|
340
|
+
return unsubscribe;
|
|
341
|
+
}, [tokenStore]);
|
|
342
|
+
const getSnapshot = useCallback(() => this._getSessionFromTokenStore(tokenStore), [tokenStore]);
|
|
343
|
+
return React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
344
|
+
}
|
|
345
|
+
async _signInToAccountWithTokens(tokens) {
|
|
346
|
+
const tokenStore = this._getOrCreateTokenStore();
|
|
347
|
+
tokenStore.set(tokens);
|
|
348
|
+
}
|
|
260
349
|
_hasPersistentTokenStore(overrideTokenStoreInit) {
|
|
261
350
|
return (overrideTokenStoreInit !== void 0 ? overrideTokenStoreInit : this._tokenStoreInit) !== null;
|
|
262
351
|
}
|
|
@@ -336,24 +425,25 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
336
425
|
});
|
|
337
426
|
},
|
|
338
427
|
async listTeams() {
|
|
339
|
-
const teams = await app._currentUserTeamsCache.getOrWait([app.
|
|
428
|
+
const teams = await app._currentUserTeamsCache.getOrWait([app._getSession()], "write-only");
|
|
340
429
|
return teams.map((json2) => app._teamFromJson(json2));
|
|
341
430
|
},
|
|
342
431
|
useTeams() {
|
|
343
|
-
const
|
|
432
|
+
const session = app._useSession();
|
|
433
|
+
const teams = useAsyncCache(app._currentUserTeamsCache, [session], "user.useTeams()");
|
|
344
434
|
return useMemo(() => teams.map((json2) => app._teamFromJson(json2)), [teams]);
|
|
345
435
|
},
|
|
346
436
|
onTeamsChange(callback) {
|
|
347
|
-
return app._currentUserTeamsCache.onChange([app.
|
|
437
|
+
return app._currentUserTeamsCache.onChange([app._getSession()], (value, oldValue) => {
|
|
348
438
|
callback(value.map((json2) => app._teamFromJson(json2)), oldValue?.map((json2) => app._teamFromJson(json2)));
|
|
349
439
|
});
|
|
350
440
|
},
|
|
351
441
|
async listPermissions(scope, options) {
|
|
352
|
-
const permissions = await app._currentUserPermissionsCache.getOrWait([app.
|
|
442
|
+
const permissions = await app._currentUserPermissionsCache.getOrWait([app._getSession(), scope.id, "team", !!options?.direct], "write-only");
|
|
353
443
|
return permissions.map((json2) => app._permissionFromJson(json2));
|
|
354
444
|
},
|
|
355
445
|
usePermissions(scope, options) {
|
|
356
|
-
const permissions =
|
|
446
|
+
const permissions = useAsyncCache(app._currentUserPermissionsCache, [app._getSession(), scope.id, "team", !!options?.direct], "user.usePermissions()");
|
|
357
447
|
return useMemo(() => permissions.map((json2) => app._permissionFromJson(json2)), [permissions]);
|
|
358
448
|
},
|
|
359
449
|
usePermission(scope, permissionId) {
|
|
@@ -381,30 +471,36 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
381
471
|
displayName: json.displayName
|
|
382
472
|
};
|
|
383
473
|
}
|
|
384
|
-
_currentUserFromJson(json,
|
|
474
|
+
_currentUserFromJson(json, session) {
|
|
385
475
|
if (json === null)
|
|
386
476
|
return null;
|
|
387
477
|
const app = this;
|
|
388
478
|
const currentUser = {
|
|
389
479
|
...this._userFromJson(json),
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
480
|
+
_internalSession: session,
|
|
481
|
+
currentSession: {
|
|
482
|
+
async getTokens() {
|
|
483
|
+
const tokens = await session.getPotentiallyExpiredTokens();
|
|
484
|
+
return {
|
|
485
|
+
accessToken: tokens?.accessToken.token ?? null,
|
|
486
|
+
refreshToken: tokens?.refreshToken?.token ?? null
|
|
487
|
+
};
|
|
488
|
+
}
|
|
393
489
|
},
|
|
394
490
|
async updateSelectedTeam(team) {
|
|
395
|
-
await app._updateUser({ selectedTeamId: team?.id ?? null },
|
|
491
|
+
await app._updateUser({ selectedTeamId: team?.id ?? null }, session);
|
|
396
492
|
},
|
|
397
493
|
update(update) {
|
|
398
|
-
return app._updateUser(update,
|
|
494
|
+
return app._updateUser(update, session);
|
|
399
495
|
},
|
|
400
496
|
signOut() {
|
|
401
|
-
return app._signOut(
|
|
497
|
+
return app._signOut(session);
|
|
402
498
|
},
|
|
403
499
|
sendVerificationEmail() {
|
|
404
|
-
return app._sendVerificationEmail(
|
|
500
|
+
return app._sendVerificationEmail(session);
|
|
405
501
|
},
|
|
406
502
|
updatePassword(options) {
|
|
407
|
-
return app._updatePassword(options,
|
|
503
|
+
return app._updatePassword(options, session);
|
|
408
504
|
}
|
|
409
505
|
};
|
|
410
506
|
if (this._isInternalProject()) {
|
|
@@ -463,13 +559,12 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
463
559
|
}
|
|
464
560
|
};
|
|
465
561
|
}
|
|
466
|
-
_createAdminInterface(forProjectId,
|
|
562
|
+
_createAdminInterface(forProjectId, session) {
|
|
467
563
|
return new StackAdminInterface({
|
|
468
564
|
baseUrl: this._interface.options.baseUrl,
|
|
469
565
|
projectId: forProjectId,
|
|
470
566
|
clientVersion,
|
|
471
|
-
|
|
472
|
-
refreshProjectOwnerTokens: async () => await this._interface.refreshAccessToken(tokenStore)
|
|
567
|
+
projectOwnerSession: session
|
|
473
568
|
});
|
|
474
569
|
}
|
|
475
570
|
get projectId() {
|
|
@@ -478,6 +573,18 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
478
573
|
get urls() {
|
|
479
574
|
return getUrls(this._urlOptions);
|
|
480
575
|
}
|
|
576
|
+
async getCrossOriginHeaders() {
|
|
577
|
+
return {
|
|
578
|
+
"x-stack-auth": JSON.stringify(await this.getCrossOriginTokenObject())
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
async getCrossOriginTokenObject() {
|
|
582
|
+
const user = await this.getUser();
|
|
583
|
+
if (!user)
|
|
584
|
+
return { accessToken: null, refreshToken: null };
|
|
585
|
+
const tokens = await user.currentSession.getTokens();
|
|
586
|
+
return tokens;
|
|
587
|
+
}
|
|
481
588
|
async _redirectTo(handlerName, options) {
|
|
482
589
|
const url = this.urls[handlerName];
|
|
483
590
|
if (!url) {
|
|
@@ -555,8 +662,8 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
555
662
|
}
|
|
556
663
|
async getUser(options) {
|
|
557
664
|
this._ensurePersistentTokenStore(options?.tokenStore);
|
|
558
|
-
const
|
|
559
|
-
const userJson = await this._currentUserCache.getOrWait([
|
|
665
|
+
const session = this._getSession(options?.tokenStore);
|
|
666
|
+
const userJson = await this._currentUserCache.getOrWait([session], "write-only");
|
|
560
667
|
if (userJson === null) {
|
|
561
668
|
switch (options?.or) {
|
|
562
669
|
case "redirect": {
|
|
@@ -571,20 +678,18 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
571
678
|
}
|
|
572
679
|
}
|
|
573
680
|
}
|
|
574
|
-
return this._currentUserFromJson(userJson,
|
|
681
|
+
return this._currentUserFromJson(userJson, session);
|
|
575
682
|
}
|
|
576
683
|
useUser(options) {
|
|
577
684
|
this._ensurePersistentTokenStore(options?.tokenStore);
|
|
578
685
|
const router = NextNavigation.useRouter();
|
|
579
|
-
const
|
|
580
|
-
const userJson =
|
|
686
|
+
const session = this._getSession(options?.tokenStore);
|
|
687
|
+
const userJson = useAsyncCache(this._currentUserCache, [session], "useUser()");
|
|
688
|
+
const triggerRedirectToSignIn = useTrigger(() => router.replace(this.urls.signIn));
|
|
581
689
|
if (userJson === null) {
|
|
582
690
|
switch (options?.or) {
|
|
583
691
|
case "redirect": {
|
|
584
|
-
|
|
585
|
-
await wait(0);
|
|
586
|
-
router.replace(this.urls.signIn);
|
|
587
|
-
});
|
|
692
|
+
triggerRedirectToSignIn();
|
|
588
693
|
suspend();
|
|
589
694
|
throw new StackAssertionError("suspend should never return");
|
|
590
695
|
}
|
|
@@ -597,19 +702,19 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
597
702
|
}
|
|
598
703
|
}
|
|
599
704
|
return useMemo(() => {
|
|
600
|
-
return this._currentUserFromJson(userJson,
|
|
601
|
-
}, [userJson,
|
|
705
|
+
return this._currentUserFromJson(userJson, session);
|
|
706
|
+
}, [userJson, session, options?.or]);
|
|
602
707
|
}
|
|
603
708
|
onUserChange(callback) {
|
|
604
709
|
this._ensurePersistentTokenStore();
|
|
605
|
-
const
|
|
606
|
-
return this._currentUserCache.onChange([
|
|
607
|
-
callback(this._currentUserFromJson(userJson,
|
|
710
|
+
const session = this._getSession();
|
|
711
|
+
return this._currentUserCache.onChange([session], (userJson) => {
|
|
712
|
+
callback(this._currentUserFromJson(userJson, session));
|
|
608
713
|
});
|
|
609
714
|
}
|
|
610
|
-
async _updateUser(update,
|
|
611
|
-
const res = await this._interface.setClientUserCustomizableData(update,
|
|
612
|
-
await this._refreshUser(
|
|
715
|
+
async _updateUser(update, session) {
|
|
716
|
+
const res = await this._interface.setClientUserCustomizableData(update, session);
|
|
717
|
+
await this._refreshUser(session);
|
|
613
718
|
return res;
|
|
614
719
|
}
|
|
615
720
|
async signInWithOAuth(provider) {
|
|
@@ -618,35 +723,38 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
618
723
|
}
|
|
619
724
|
async signInWithCredential(options) {
|
|
620
725
|
this._ensurePersistentTokenStore();
|
|
621
|
-
const
|
|
622
|
-
const
|
|
623
|
-
if (!
|
|
624
|
-
await this.
|
|
726
|
+
const session = this._getSession();
|
|
727
|
+
const result = await this._interface.signInWithCredential(options.email, options.password, session);
|
|
728
|
+
if (!(result instanceof KnownError)) {
|
|
729
|
+
await this._signInToAccountWithTokens(result);
|
|
730
|
+
return await this.redirectToAfterSignIn({ replace: true });
|
|
625
731
|
}
|
|
626
|
-
return
|
|
732
|
+
return result;
|
|
627
733
|
}
|
|
628
734
|
async signUpWithCredential(options) {
|
|
629
735
|
this._ensurePersistentTokenStore();
|
|
630
|
-
const
|
|
736
|
+
const session = this._getSession();
|
|
631
737
|
const emailVerificationRedirectUrl = constructRedirectUrl(this.urls.emailVerification);
|
|
632
|
-
const
|
|
738
|
+
const result = await this._interface.signUpWithCredential(
|
|
633
739
|
options.email,
|
|
634
740
|
options.password,
|
|
635
741
|
emailVerificationRedirectUrl,
|
|
636
|
-
|
|
742
|
+
session
|
|
637
743
|
);
|
|
638
|
-
if (!
|
|
639
|
-
await this.
|
|
744
|
+
if (!(result instanceof KnownError)) {
|
|
745
|
+
await this._signInToAccountWithTokens(result);
|
|
746
|
+
return await this.redirectToAfterSignUp({ replace: true });
|
|
640
747
|
}
|
|
641
|
-
return
|
|
748
|
+
return result;
|
|
642
749
|
}
|
|
643
750
|
async signInWithMagicLink(code) {
|
|
644
751
|
this._ensurePersistentTokenStore();
|
|
645
|
-
const
|
|
646
|
-
const result = await this._interface.signInWithMagicLink(code,
|
|
752
|
+
const session = this._getSession();
|
|
753
|
+
const result = await this._interface.signInWithMagicLink(code, session);
|
|
647
754
|
if (result instanceof KnownError) {
|
|
648
755
|
return result;
|
|
649
756
|
}
|
|
757
|
+
await this._signInToAccountWithTokens(result);
|
|
650
758
|
if (result.newUser) {
|
|
651
759
|
await this.redirectToAfterSignUp({ replace: true });
|
|
652
760
|
} else {
|
|
@@ -655,9 +763,9 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
655
763
|
}
|
|
656
764
|
async callOAuthCallback() {
|
|
657
765
|
this._ensurePersistentTokenStore();
|
|
658
|
-
const
|
|
659
|
-
const result = await callOAuthCallback(this._interface, tokenStore, this.urls.oauthCallback);
|
|
766
|
+
const result = await callOAuthCallback(this._interface, this.urls.oauthCallback);
|
|
660
767
|
if (result) {
|
|
768
|
+
await this._signInToAccountWithTokens(result);
|
|
661
769
|
if (result.newUser) {
|
|
662
770
|
await this.redirectToAfterSignUp({ replace: true });
|
|
663
771
|
return true;
|
|
@@ -668,16 +776,16 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
668
776
|
}
|
|
669
777
|
return false;
|
|
670
778
|
}
|
|
671
|
-
async _signOut(
|
|
672
|
-
await this._interface.signOut(
|
|
779
|
+
async _signOut(session) {
|
|
780
|
+
await this._interface.signOut(session);
|
|
673
781
|
await this.redirectToAfterSignOut();
|
|
674
782
|
}
|
|
675
|
-
async _sendVerificationEmail(
|
|
783
|
+
async _sendVerificationEmail(session) {
|
|
676
784
|
const emailVerificationRedirectUrl = constructRedirectUrl(this.urls.emailVerification);
|
|
677
|
-
return await this._interface.sendVerificationEmail(emailVerificationRedirectUrl,
|
|
785
|
+
return await this._interface.sendVerificationEmail(emailVerificationRedirectUrl, session);
|
|
678
786
|
}
|
|
679
|
-
async _updatePassword(options,
|
|
680
|
-
return await this._interface.updatePassword(options,
|
|
787
|
+
async _updatePassword(options, session) {
|
|
788
|
+
return await this._interface.updatePassword(options, session);
|
|
681
789
|
}
|
|
682
790
|
async signOut() {
|
|
683
791
|
const user = await this.getUser();
|
|
@@ -689,64 +797,64 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
689
797
|
return await this._currentProjectCache.getOrWait([], "write-only");
|
|
690
798
|
}
|
|
691
799
|
useProject() {
|
|
692
|
-
return
|
|
800
|
+
return useAsyncCache(this._currentProjectCache, [], "useProject()");
|
|
693
801
|
}
|
|
694
802
|
onProjectChange(callback) {
|
|
695
803
|
return this._currentProjectCache.onChange([], callback);
|
|
696
804
|
}
|
|
697
805
|
async _listOwnedProjects() {
|
|
698
806
|
this._ensureInternalProject();
|
|
699
|
-
const
|
|
700
|
-
const json = await this._ownedProjectsCache.getOrWait([
|
|
807
|
+
const session = this._getSession();
|
|
808
|
+
const json = await this._ownedProjectsCache.getOrWait([session], "write-only");
|
|
701
809
|
return json.map((j) => this._projectAdminFromJson(
|
|
702
810
|
j,
|
|
703
|
-
this._createAdminInterface(j.id,
|
|
704
|
-
() => this._refreshOwnedProjects(
|
|
811
|
+
this._createAdminInterface(j.id, session),
|
|
812
|
+
() => this._refreshOwnedProjects(session)
|
|
705
813
|
));
|
|
706
814
|
}
|
|
707
815
|
_useOwnedProjects() {
|
|
708
816
|
this._ensureInternalProject();
|
|
709
|
-
const
|
|
710
|
-
const json =
|
|
817
|
+
const session = this._getSession();
|
|
818
|
+
const json = useAsyncCache(this._ownedProjectsCache, [session], "useOwnedProjects()");
|
|
711
819
|
return useMemo(() => json.map((j) => this._projectAdminFromJson(
|
|
712
820
|
j,
|
|
713
|
-
this._createAdminInterface(j.id,
|
|
714
|
-
() => this._refreshOwnedProjects(
|
|
821
|
+
this._createAdminInterface(j.id, session),
|
|
822
|
+
() => this._refreshOwnedProjects(session)
|
|
715
823
|
)), [json]);
|
|
716
824
|
}
|
|
717
825
|
_onOwnedProjectsChange(callback) {
|
|
718
826
|
this._ensureInternalProject();
|
|
719
|
-
const
|
|
720
|
-
return this._ownedProjectsCache.onChange([
|
|
827
|
+
const session = this._getSession();
|
|
828
|
+
return this._ownedProjectsCache.onChange([session], (projects) => {
|
|
721
829
|
callback(projects.map((j) => this._projectAdminFromJson(
|
|
722
830
|
j,
|
|
723
|
-
this._createAdminInterface(j.id,
|
|
724
|
-
() => this._refreshOwnedProjects(
|
|
831
|
+
this._createAdminInterface(j.id, session),
|
|
832
|
+
() => this._refreshOwnedProjects(session)
|
|
725
833
|
)));
|
|
726
834
|
});
|
|
727
835
|
}
|
|
728
836
|
async _createProject(newProject) {
|
|
729
837
|
this._ensureInternalProject();
|
|
730
|
-
const
|
|
731
|
-
const json = await this._interface.createProject(newProject,
|
|
838
|
+
const session = this._getSession();
|
|
839
|
+
const json = await this._interface.createProject(newProject, session);
|
|
732
840
|
const res = this._projectAdminFromJson(
|
|
733
841
|
json,
|
|
734
|
-
this._createAdminInterface(json.id,
|
|
735
|
-
() => this._refreshOwnedProjects(
|
|
842
|
+
this._createAdminInterface(json.id, session),
|
|
843
|
+
() => this._refreshOwnedProjects(session)
|
|
736
844
|
);
|
|
737
|
-
await this._refreshOwnedProjects(
|
|
845
|
+
await this._refreshOwnedProjects(session);
|
|
738
846
|
return res;
|
|
739
847
|
}
|
|
740
|
-
async _refreshUser(
|
|
741
|
-
await this._currentUserCache.refresh([
|
|
848
|
+
async _refreshUser(session) {
|
|
849
|
+
await this._currentUserCache.refresh([session]);
|
|
742
850
|
}
|
|
743
851
|
async _refreshUsers() {
|
|
744
852
|
}
|
|
745
853
|
async _refreshProject() {
|
|
746
854
|
await this._currentProjectCache.refresh([]);
|
|
747
855
|
}
|
|
748
|
-
async _refreshOwnedProjects(
|
|
749
|
-
await this._ownedProjectsCache.refresh([
|
|
856
|
+
async _refreshOwnedProjects(session) {
|
|
857
|
+
await this._ownedProjectsCache.refresh([session]);
|
|
750
858
|
}
|
|
751
859
|
static get [stackAppInternalsSymbol]() {
|
|
752
860
|
return {
|
|
@@ -785,15 +893,15 @@ var _StackClientAppImpl = class __StackClientAppImpl {
|
|
|
785
893
|
};
|
|
786
894
|
},
|
|
787
895
|
setCurrentUser: (userJsonPromise) => {
|
|
788
|
-
runAsynchronously(this._currentUserCache.forceSetCachedValueAsync([this.
|
|
896
|
+
runAsynchronously(this._currentUserCache.forceSetCachedValueAsync([this._getSession()], userJsonPromise));
|
|
789
897
|
}
|
|
790
898
|
};
|
|
791
899
|
}
|
|
792
900
|
};
|
|
793
901
|
var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
794
902
|
// TODO override the client user cache to use the server user cache, so we save some requests
|
|
795
|
-
_currentServerUserCache =
|
|
796
|
-
const user = await this._interface.getServerUserByToken(
|
|
903
|
+
_currentServerUserCache = createCacheBySession(async (session) => {
|
|
904
|
+
const user = await this._interface.getServerUserByToken(session);
|
|
797
905
|
return Result.or(user, null);
|
|
798
906
|
});
|
|
799
907
|
_serverUsersCache = createCache(async () => {
|
|
@@ -884,15 +992,15 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
884
992
|
});
|
|
885
993
|
},
|
|
886
994
|
async listTeams() {
|
|
887
|
-
const teams = await app._serverTeamsCache.getOrWait([app.
|
|
995
|
+
const teams = await app._serverTeamsCache.getOrWait([app._getSession()], "write-only");
|
|
888
996
|
return teams.map((json2) => app._serverTeamFromJson(json2));
|
|
889
997
|
},
|
|
890
998
|
useTeams() {
|
|
891
|
-
const teams =
|
|
999
|
+
const teams = useAsyncCache(app._serverTeamsCache, [app._getSession()], "user.useTeams()");
|
|
892
1000
|
return useMemo(() => teams.map((json2) => app._serverTeamFromJson(json2)), [teams]);
|
|
893
1001
|
},
|
|
894
1002
|
onTeamsChange(callback) {
|
|
895
|
-
return app._serverTeamsCache.onChange([app.
|
|
1003
|
+
return app._serverTeamsCache.onChange([app._getSession()], (value, oldValue) => {
|
|
896
1004
|
callback(value.map((json2) => app._serverTeamFromJson(json2)), oldValue?.map((json2) => app._serverTeamFromJson(json2)));
|
|
897
1005
|
});
|
|
898
1006
|
},
|
|
@@ -901,7 +1009,7 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
901
1009
|
return permissions.map((json2) => app._serverPermissionFromJson(json2));
|
|
902
1010
|
},
|
|
903
1011
|
usePermissions(scope, options) {
|
|
904
|
-
const permissions =
|
|
1012
|
+
const permissions = useAsyncCache(app._serverTeamUserPermissionsCache, [scope.id, json.id, "team", !!options?.direct], "user.usePermissions()");
|
|
905
1013
|
return useMemo(() => permissions.map((json2) => app._serverPermissionFromJson(json2)), [permissions]);
|
|
906
1014
|
},
|
|
907
1015
|
usePermission(scope, permissionId) {
|
|
@@ -921,20 +1029,26 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
921
1029
|
}
|
|
922
1030
|
};
|
|
923
1031
|
}
|
|
924
|
-
_currentServerUserFromJson(json,
|
|
1032
|
+
_currentServerUserFromJson(json, session) {
|
|
925
1033
|
if (json === null)
|
|
926
1034
|
return null;
|
|
927
1035
|
const app = this;
|
|
928
1036
|
const nonCurrentServerUser = this._serverUserFromJson(json);
|
|
929
1037
|
const currentUser = {
|
|
930
1038
|
...nonCurrentServerUser,
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
1039
|
+
_internalSession: session,
|
|
1040
|
+
currentSession: {
|
|
1041
|
+
async getTokens() {
|
|
1042
|
+
const tokens = await session.getPotentiallyExpiredTokens();
|
|
1043
|
+
return {
|
|
1044
|
+
accessToken: tokens?.accessToken.token ?? null,
|
|
1045
|
+
refreshToken: tokens?.refreshToken?.token ?? null
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
934
1048
|
},
|
|
935
1049
|
async delete() {
|
|
936
1050
|
const res = await nonCurrentServerUser.delete();
|
|
937
|
-
await app._refreshUser(
|
|
1051
|
+
await app._refreshUser(session);
|
|
938
1052
|
return res;
|
|
939
1053
|
},
|
|
940
1054
|
async updateSelectedTeam(team) {
|
|
@@ -942,20 +1056,20 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
942
1056
|
},
|
|
943
1057
|
async update(update) {
|
|
944
1058
|
const res = await nonCurrentServerUser.update(update);
|
|
945
|
-
await app._refreshUser(
|
|
1059
|
+
await app._refreshUser(session);
|
|
946
1060
|
return res;
|
|
947
1061
|
},
|
|
948
1062
|
signOut() {
|
|
949
|
-
return app._signOut(
|
|
1063
|
+
return app._signOut(session);
|
|
950
1064
|
},
|
|
951
1065
|
getClientUser() {
|
|
952
|
-
return app._currentUserFromJson(json,
|
|
1066
|
+
return app._currentUserFromJson(json, session);
|
|
953
1067
|
},
|
|
954
1068
|
sendVerificationEmail() {
|
|
955
|
-
return app._sendVerificationEmail(
|
|
1069
|
+
return app._sendVerificationEmail(session);
|
|
956
1070
|
},
|
|
957
1071
|
updatePassword(options) {
|
|
958
|
-
return app._updatePassword(options,
|
|
1072
|
+
return app._updatePassword(options, session);
|
|
959
1073
|
}
|
|
960
1074
|
};
|
|
961
1075
|
if (this._isInternalProject()) {
|
|
@@ -1013,7 +1127,7 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1013
1127
|
await app._serverTeamsCache.refresh([]);
|
|
1014
1128
|
},
|
|
1015
1129
|
useMembers() {
|
|
1016
|
-
const result =
|
|
1130
|
+
const result = useAsyncCache(app._serverTeamMembersCache, [json.id], "team.useUsers()");
|
|
1017
1131
|
return useMemo(() => result.map((u) => app._serverTeamMemberFromJson(u)), [result]);
|
|
1018
1132
|
},
|
|
1019
1133
|
async addUser(userId) {
|
|
@@ -1037,9 +1151,9 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1037
1151
|
}
|
|
1038
1152
|
async getServerUser() {
|
|
1039
1153
|
this._ensurePersistentTokenStore();
|
|
1040
|
-
const
|
|
1041
|
-
const userJson = await this._currentServerUserCache.getOrWait([
|
|
1042
|
-
return this._currentServerUserFromJson(userJson,
|
|
1154
|
+
const session = this._getSession();
|
|
1155
|
+
const userJson = await this._currentServerUserCache.getOrWait([session], "write-only");
|
|
1156
|
+
return this._currentServerUserFromJson(userJson, session);
|
|
1043
1157
|
}
|
|
1044
1158
|
async getServerUserById(userId) {
|
|
1045
1159
|
const json = await this._serverUserCache.getOrWait([userId], "write-only");
|
|
@@ -1047,20 +1161,20 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1047
1161
|
}
|
|
1048
1162
|
useServerUser(options) {
|
|
1049
1163
|
this._ensurePersistentTokenStore();
|
|
1050
|
-
const
|
|
1051
|
-
const userJson =
|
|
1164
|
+
const session = this._getSession();
|
|
1165
|
+
const userJson = useAsyncCache(this._currentServerUserCache, [session], "useServerUser()");
|
|
1052
1166
|
return useMemo(() => {
|
|
1053
1167
|
if (options?.required && userJson === null) {
|
|
1054
1168
|
use(this.redirectToSignIn());
|
|
1055
1169
|
}
|
|
1056
|
-
return this._currentServerUserFromJson(userJson,
|
|
1057
|
-
}, [userJson,
|
|
1170
|
+
return this._currentServerUserFromJson(userJson, session);
|
|
1171
|
+
}, [userJson, session, options?.required]);
|
|
1058
1172
|
}
|
|
1059
1173
|
onServerUserChange(callback) {
|
|
1060
1174
|
this._ensurePersistentTokenStore();
|
|
1061
|
-
const
|
|
1062
|
-
return this._currentServerUserCache.onChange([
|
|
1063
|
-
callback(this._currentServerUserFromJson(userJson,
|
|
1175
|
+
const session = this._getSession();
|
|
1176
|
+
return this._currentServerUserCache.onChange([session], (userJson) => {
|
|
1177
|
+
callback(this._currentServerUserFromJson(userJson, session));
|
|
1064
1178
|
});
|
|
1065
1179
|
}
|
|
1066
1180
|
async listServerUsers() {
|
|
@@ -1068,7 +1182,7 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1068
1182
|
return json.map((j) => this._serverUserFromJson(j));
|
|
1069
1183
|
}
|
|
1070
1184
|
useServerUsers() {
|
|
1071
|
-
const json =
|
|
1185
|
+
const json = useAsyncCache(this._serverUsersCache, [], "useServerUsers()");
|
|
1072
1186
|
return useMemo(() => {
|
|
1073
1187
|
return json.map((j) => this._serverUserFromJson(j));
|
|
1074
1188
|
}, [json]);
|
|
@@ -1082,7 +1196,7 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1082
1196
|
return await this._serverTeamPermissionDefinitionsCache.getOrWait([], "write-only");
|
|
1083
1197
|
}
|
|
1084
1198
|
usePermissionDefinitions() {
|
|
1085
|
-
return
|
|
1199
|
+
return useAsyncCache(this._serverTeamPermissionDefinitionsCache, [], "usePermissions()");
|
|
1086
1200
|
}
|
|
1087
1201
|
_serverPermissionFromJson(json) {
|
|
1088
1202
|
return {
|
|
@@ -1093,7 +1207,7 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1093
1207
|
};
|
|
1094
1208
|
}
|
|
1095
1209
|
async createPermissionDefinition(data) {
|
|
1096
|
-
const permission =
|
|
1210
|
+
const permission = this._serverPermissionFromJson(await this._interface.createPermissionDefinition(data));
|
|
1097
1211
|
await this._serverTeamPermissionDefinitionsCache.refresh([]);
|
|
1098
1212
|
return permission;
|
|
1099
1213
|
}
|
|
@@ -1115,7 +1229,7 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1115
1229
|
return this._serverTeamFromJson(team);
|
|
1116
1230
|
}
|
|
1117
1231
|
useTeams() {
|
|
1118
|
-
const teams =
|
|
1232
|
+
const teams = useAsyncCache(this._serverTeamsCache, [], "useServerTeams()");
|
|
1119
1233
|
return useMemo(() => {
|
|
1120
1234
|
return teams.map((t) => this._serverTeamFromJson(t));
|
|
1121
1235
|
}, [teams]);
|
|
@@ -1130,10 +1244,10 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1130
1244
|
return teams.find((t) => t.id === teamId) ?? null;
|
|
1131
1245
|
}, [teams, teamId]);
|
|
1132
1246
|
}
|
|
1133
|
-
async _refreshUser(
|
|
1247
|
+
async _refreshUser(session) {
|
|
1134
1248
|
await Promise.all([
|
|
1135
|
-
super._refreshUser(
|
|
1136
|
-
this._currentServerUserCache.refresh([
|
|
1249
|
+
super._refreshUser(session),
|
|
1250
|
+
this._currentServerUserCache.refresh([session])
|
|
1137
1251
|
]);
|
|
1138
1252
|
}
|
|
1139
1253
|
async _refreshUsers() {
|
|
@@ -1143,7 +1257,7 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
|
|
|
1143
1257
|
]);
|
|
1144
1258
|
}
|
|
1145
1259
|
useEmailTemplates() {
|
|
1146
|
-
return
|
|
1260
|
+
return useAsyncCache(this._serverEmailTemplatesCache, [], "useEmailTemplates()");
|
|
1147
1261
|
}
|
|
1148
1262
|
async listEmailTemplates() {
|
|
1149
1263
|
return await this._serverEmailTemplatesCache.getOrWait([], "write-only");
|
|
@@ -1170,9 +1284,8 @@ var _StackAdminAppImpl = class extends _StackServerAppImpl {
|
|
|
1170
1284
|
baseUrl: options.baseUrl ?? getDefaultBaseUrl(),
|
|
1171
1285
|
projectId: options.projectId ?? getDefaultProjectId(),
|
|
1172
1286
|
clientVersion,
|
|
1173
|
-
..."
|
|
1174
|
-
|
|
1175
|
-
refreshProjectOwnerTokens: options.refreshProjectOwnerTokens
|
|
1287
|
+
..."projectOwnerSession" in options ? {
|
|
1288
|
+
projectOwnerSession: options.projectOwnerSession
|
|
1176
1289
|
} : {
|
|
1177
1290
|
publishableClientKey: options.publishableClientKey ?? getDefaultPublishableClientKey(),
|
|
1178
1291
|
secretServerKey: options.secretServerKey ?? getDefaultSecretServerKey(),
|
|
@@ -1232,7 +1345,7 @@ var _StackAdminAppImpl = class extends _StackServerAppImpl {
|
|
|
1232
1345
|
);
|
|
1233
1346
|
}
|
|
1234
1347
|
useProjectAdmin() {
|
|
1235
|
-
const json =
|
|
1348
|
+
const json = useAsyncCache(this._adminProjectCache, [], "useProjectAdmin()");
|
|
1236
1349
|
return useMemo(() => this._projectAdminFromJson(
|
|
1237
1350
|
json,
|
|
1238
1351
|
this._interface,
|
|
@@ -1253,7 +1366,7 @@ var _StackAdminAppImpl = class extends _StackServerAppImpl {
|
|
|
1253
1366
|
return json.map((j) => this._createApiKeySetFromJson(j));
|
|
1254
1367
|
}
|
|
1255
1368
|
useApiKeySets() {
|
|
1256
|
-
const json =
|
|
1369
|
+
const json = useAsyncCache(this._apiKeySetsCache, [], "useApiKeySets()");
|
|
1257
1370
|
return useMemo(() => {
|
|
1258
1371
|
return json.map((j) => this._createApiKeySetFromJson(j));
|
|
1259
1372
|
}, [json]);
|