@sanity/sdk 2.3.0 → 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +4 -0
- package/dist/index.js +41 -22
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/auth/authStore.test.ts +12 -20
- package/src/auth/authStore.ts +46 -15
- package/src/auth/studioModeAuth.test.ts +4 -4
- package/src/auth/studioModeAuth.ts +4 -5
- package/src/auth/subscribeToStateAndFetchCurrentUser.ts +20 -9
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -404,31 +404,34 @@ async function checkForCookieAuth(projectId, clientFactory) {
|
|
|
404
404
|
return !1;
|
|
405
405
|
}
|
|
406
406
|
}
|
|
407
|
-
function getStudioTokenFromLocalStorage(storageArea,
|
|
408
|
-
|
|
409
|
-
const studioStorageKey = `__studio_auth_token_${projectId}`;
|
|
410
|
-
return getTokenFromStorage(storageArea, studioStorageKey) || null;
|
|
407
|
+
function getStudioTokenFromLocalStorage(storageArea, storageKey) {
|
|
408
|
+
return !storageArea || !storageKey ? null : getTokenFromStorage(storageArea, storageKey) || null;
|
|
411
409
|
}
|
|
412
410
|
const subscribeToStateAndFetchCurrentUser = ({
|
|
413
|
-
state
|
|
411
|
+
state,
|
|
412
|
+
instance
|
|
414
413
|
}) => {
|
|
415
|
-
const { clientFactory, apiHost } = state.get().options;
|
|
414
|
+
const { clientFactory, apiHost } = state.get().options, useProjectHostname = !!instance.config.studioMode?.enabled, projectId = instance.config.projectId;
|
|
416
415
|
return state.observable.pipe(
|
|
417
|
-
map(({ authState }) => authState),
|
|
416
|
+
map(({ authState, options }) => ({ authState, authMethod: options.authMethod })),
|
|
418
417
|
filter(
|
|
419
|
-
(
|
|
418
|
+
(value) => value.authState.type === AuthStateType.LOGGED_IN && !value.authState.currentUser
|
|
420
419
|
),
|
|
421
|
-
map((
|
|
422
|
-
distinctUntilChanged(
|
|
420
|
+
map((value) => ({ token: value.authState.token, authMethod: value.authMethod })),
|
|
421
|
+
distinctUntilChanged(
|
|
422
|
+
(prev, curr) => prev.token === curr.token && prev.authMethod === curr.authMethod
|
|
423
|
+
)
|
|
423
424
|
).pipe(
|
|
424
425
|
map(
|
|
425
|
-
(token) => clientFactory({
|
|
426
|
+
({ token, authMethod }) => clientFactory({
|
|
426
427
|
apiVersion: DEFAULT_API_VERSION$1,
|
|
427
428
|
requestTagPrefix: REQUEST_TAG_PREFIX,
|
|
428
|
-
token,
|
|
429
|
+
token: authMethod === "cookie" ? void 0 : token,
|
|
429
430
|
ignoreBrowserTokenWarning: !0,
|
|
430
|
-
useProjectHostname
|
|
431
|
+
useProjectHostname,
|
|
431
432
|
useCdn: !1,
|
|
433
|
+
...authMethod === "cookie" ? { withCredentials: !0 } : {},
|
|
434
|
+
...useProjectHostname && projectId ? { projectId } : {},
|
|
432
435
|
...apiHost && { apiHost }
|
|
433
436
|
})
|
|
434
437
|
),
|
|
@@ -473,8 +476,8 @@ const authStore = {
|
|
|
473
476
|
clientFactory = createClient,
|
|
474
477
|
initialLocationHref = getDefaultLocation()
|
|
475
478
|
} = instance.config.auth ?? {};
|
|
476
|
-
let storageArea = instance.config.auth?.storageArea;
|
|
477
|
-
const
|
|
479
|
+
let storageArea = instance.config.auth?.storageArea, storageKey = "__sanity_auth_token";
|
|
480
|
+
const studioModeEnabled = instance.config.studioMode?.enabled;
|
|
478
481
|
let loginDomain = "https://www.sanity.io";
|
|
479
482
|
try {
|
|
480
483
|
apiHost && new URL(apiHost).hostname.endsWith(".sanity.work") && (loginDomain = "https://www.sanity.work");
|
|
@@ -492,13 +495,15 @@ const authStore = {
|
|
|
492
495
|
} catch (err) {
|
|
493
496
|
console.error("Failed to parse dashboard context from initial location:", err);
|
|
494
497
|
}
|
|
495
|
-
isInDashboard || (storageArea = storageArea ?? getDefaultStorage());
|
|
498
|
+
(!isInDashboard || studioModeEnabled) && (storageArea = storageArea ?? getDefaultStorage());
|
|
496
499
|
let token, authMethod;
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
+
if (studioModeEnabled) {
|
|
501
|
+
const studioStorageKey = `__studio_auth_token_${instance.config.projectId ?? ""}`;
|
|
502
|
+
storageKey = studioStorageKey, token = getStudioTokenFromLocalStorage(storageArea, studioStorageKey), token && (authMethod = "localstorage");
|
|
503
|
+
} else
|
|
504
|
+
token = getTokenFromStorage(storageArea, storageKey), token && (authMethod = "localstorage");
|
|
500
505
|
let authState;
|
|
501
|
-
return providedToken ? authState = { type: AuthStateType.LOGGED_IN, token: providedToken, currentUser: null } : getAuthCode(callbackUrl, initialLocationHref) || getTokenFromLocation(initialLocationHref) ? authState = { type: AuthStateType.LOGGING_IN, isExchangingToken: !1 } : token && !isInDashboard ? authState = { type: AuthStateType.LOGGED_IN, token, currentUser: null } : authState = { type: AuthStateType.LOGGED_OUT, isDestroyingSession: !1 }, {
|
|
506
|
+
return providedToken ? authState = { type: AuthStateType.LOGGED_IN, token: providedToken, currentUser: null } : token && studioModeEnabled ? authState = { type: AuthStateType.LOGGED_IN, token: token ?? "", currentUser: null } : getAuthCode(callbackUrl, initialLocationHref) || getTokenFromLocation(initialLocationHref) ? authState = { type: AuthStateType.LOGGING_IN, isExchangingToken: !1 } : token && !isInDashboard && !studioModeEnabled ? authState = { type: AuthStateType.LOGGED_IN, token, currentUser: null } : authState = { type: AuthStateType.LOGGED_OUT, isDestroyingSession: !1 }, {
|
|
502
507
|
authState,
|
|
503
508
|
dashboardContext,
|
|
504
509
|
options: {
|
|
@@ -517,7 +522,21 @@ const authStore = {
|
|
|
517
522
|
},
|
|
518
523
|
initialize(context) {
|
|
519
524
|
const subscriptions = [];
|
|
520
|
-
|
|
525
|
+
subscriptions.push(subscribeToStateAndFetchCurrentUser(context)), context.state.get().options?.storageArea && subscriptions.push(subscribeToStorageEventsAndSetToken(context));
|
|
526
|
+
try {
|
|
527
|
+
const { instance, state } = context, studioModeEnabled = !!instance.config.studioMode?.enabled, token = state.get().authState?.type === AuthStateType.LOGGED_IN ? state.get().authState.token : null;
|
|
528
|
+
if (studioModeEnabled && !token) {
|
|
529
|
+
const projectId = instance.config.projectId, clientFactory = state.get().options.clientFactory;
|
|
530
|
+
checkForCookieAuth(projectId, clientFactory).then((isCookieAuthEnabled) => {
|
|
531
|
+
isCookieAuthEnabled && state.set("enableCookieAuth", (prev) => ({
|
|
532
|
+
options: { ...prev.options, authMethod: "cookie" },
|
|
533
|
+
authState: prev.authState.type === AuthStateType.LOGGED_IN ? prev.authState : { type: AuthStateType.LOGGED_IN, token: "", currentUser: null }
|
|
534
|
+
}));
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
} catch {
|
|
538
|
+
}
|
|
539
|
+
return tokenRefresherRunning || (tokenRefresherRunning = !0, subscriptions.push(refreshStampedToken(context))), () => {
|
|
521
540
|
for (const subscription of subscriptions)
|
|
522
541
|
subscription.unsubscribe();
|
|
523
542
|
};
|
|
@@ -3883,7 +3902,7 @@ function getCorsErrorProjectId(error) {
|
|
|
3883
3902
|
const projMatch = (error.message || "").match(/manage\/project\/([^/?#]+)/);
|
|
3884
3903
|
return projMatch ? projMatch[1] : null;
|
|
3885
3904
|
}
|
|
3886
|
-
var version = "2.3.
|
|
3905
|
+
var version = "2.3.1";
|
|
3887
3906
|
const CORE_SDK_VERSION = getEnv("PKG_VERSION") || `${version}-development`;
|
|
3888
3907
|
export {
|
|
3889
3908
|
AuthStateType,
|