@jskit-ai/auth-web 0.2.6 → 0.2.7

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.
Files changed (34) hide show
  1. package/fixtures/sign-in/index.html +12 -0
  2. package/fixtures/sign-in/main.js +97 -0
  3. package/fixtures/sign-in/vite.config.mjs +9 -0
  4. package/locales/en.json +0 -1
  5. package/package.json +10 -5
  6. package/src/client/browser.js +37 -0
  7. package/src/client/composables/loginView/oauthCallbackUrl.js +7 -4
  8. package/src/client/composables/loginView/rememberedAccountStorage.js +6 -4
  9. package/src/client/composables/loginView/useLoginViewActions.js +19 -24
  10. package/src/client/composables/loginView/useLoginViewState.js +10 -18
  11. package/src/client/composables/loginView/useLoginViewValidation.js +2 -1
  12. package/src/client/composables/loginView/useSessionRecheckOnShow.js +5 -2
  13. package/src/client/composables/useDefaultResetPasswordView.js +109 -0
  14. package/src/client/composables/useDefaultSignOutView.js +2 -10
  15. package/src/client/providers/AuthWebClientProvider.js +20 -2
  16. package/src/client/runtime/authClient.js +67 -33
  17. package/src/client/runtime/authGuardRuntime.js +51 -99
  18. package/src/client/runtime/inject.js +38 -30
  19. package/src/client/runtime/oauthCallbackRuntime.js +8 -12
  20. package/src/client/runtime/oauthLaunchClient.js +18 -23
  21. package/src/client/runtime/passwordRecoveryCallbackRuntime.js +5 -3
  22. package/src/client/runtime/popupOAuthLaunchClient.js +70 -59
  23. package/src/client/runtime/useLoginView.js +7 -4
  24. package/src/client/runtime/useSignOut.js +51 -42
  25. package/src/client/stores/useAuthStore.js +13 -17
  26. package/src/client/types.js +86 -0
  27. package/src/client/views/AuthProfileMenuLinkItem.vue +2 -4
  28. package/src/client/views/DefaultLoginView.vue +4 -36
  29. package/src/client/views/DefaultResetPasswordView.vue +15 -97
  30. package/test/clientSurface.test.js +2 -2
  31. package/test/loginViewActions.test.js +1 -0
  32. package/test/popupOAuthLaunchClient.test.js +2 -2
  33. package/test/provider.test.js +4 -2
  34. package/test/signIn.browser.test.js +365 -0
@@ -12,29 +12,66 @@ import {
12
12
  } from "./inject.js";
13
13
  import { useAuthStore } from "../stores/useAuthStore.js";
14
14
 
15
+ function releaseNothing() {}
16
+
17
+ /**
18
+ * The realtime side of an application without `client.realtime`: no events, and no reconnect to prepare.
19
+ * @type {import("../types.js").AuthRealtime}
20
+ */
21
+ const NO_AUTH_REALTIME = Object.freeze({
22
+ socket: Object.freeze({
23
+ on() {},
24
+ off() {}
25
+ }),
26
+ setBeforeReconnect() {
27
+ return releaseNothing;
28
+ }
29
+ });
30
+
15
31
  function createMobileCallbackCompleter() {
32
+ /**
33
+ * @param {{
34
+ * url?: string,
35
+ * fallbackReturnTo?: string,
36
+ * placementContext?: Record<string, any> | null,
37
+ * defaultProvider?: string,
38
+ * request?: import("../types.js").AuthRequest,
39
+ * refreshSession?: () => Promise<any>
40
+ * }} options
41
+ */
42
+ async function completeFromUrl({
43
+ url = "",
44
+ fallbackReturnTo = "/",
45
+ placementContext = null,
46
+ defaultProvider = "",
47
+ request,
48
+ refreshSession
49
+ }) {
50
+ return completeOAuthCallbackFromUrl({
51
+ url,
52
+ fallbackReturnTo,
53
+ allowedReturnToOrigins: resolveAllowedOriginsFromPlacementContext(placementContext),
54
+ defaultProvider,
55
+ request,
56
+ refreshSession
57
+ });
58
+ }
59
+
16
60
  return Object.freeze({
17
- async completeFromUrl({
18
- url = "",
19
- fallbackReturnTo = "/",
20
- placementContext = null,
21
- defaultProvider = "",
22
- request = undefined,
23
- refreshSession = async () => null
24
- } = {}) {
25
- return completeOAuthCallbackFromUrl({
26
- url,
27
- fallbackReturnTo,
28
- allowedReturnToOrigins: resolveAllowedOriginsFromPlacementContext(placementContext),
29
- defaultProvider,
30
- ...(typeof request === "function" ? { request } : {}),
31
- refreshSession
32
- });
33
- }
61
+ completeFromUrl
34
62
  });
35
63
  }
36
64
 
37
- function createAuthClient({ mobile = null, pinia, realtime = null, shell, vueApp } = {}) {
65
+ /**
66
+ * @param {{
67
+ * mobile?: import("../types.js").AuthMobile | null,
68
+ * pinia: import("@jskit-ai/kernel/client/types").ClientPinia,
69
+ * realtime: import("../types.js").AuthRealtime,
70
+ * shell: import("../types.js").AuthShell,
71
+ * vueApp: import("@jskit-ai/kernel/client/types").ClientVueApp
72
+ * }} options
73
+ */
74
+ function createAuthClient({ mobile, pinia, realtime, shell, vueApp }) {
38
75
  const placementRuntime = shell.placement;
39
76
  const loginRouteTarget = resolveSurfaceNavigationTargetFromPlacementContext(placementRuntime.getContext(), {
40
77
  path: "/auth/login",
@@ -43,17 +80,14 @@ function createAuthClient({ mobile = null, pinia, realtime = null, shell, vueApp
43
80
  const guard = createAuthGuardRuntime({
44
81
  loginRoute: loginRouteTarget.href,
45
82
  placementRuntime,
46
- realtimeSocket: realtime?.socket || null
83
+ realtimeSocket: realtime.socket
47
84
  });
48
85
  const mobileCallback = createMobileCallbackCompleter();
49
86
  const oauthLaunch = mobile?.oauthLaunch || createPopupOAuthLaunchClient();
50
- let stopAuthRefresh = null;
51
- let releaseBeforeReconnect = null;
87
+ let stopAuthRefresh = releaseNothing;
88
+ let releaseBeforeReconnect = releaseNothing;
52
89
 
53
90
  async function initialize() {
54
- if (!pinia) {
55
- throw new Error("AuthWebClientProvider requires Pinia installed in the client app.");
56
- }
57
91
  const authStore = useAuthStore(pinia);
58
92
  authStore.attachRuntime(guard);
59
93
  await authStore.initialize();
@@ -62,17 +96,17 @@ function createAuthClient({ mobile = null, pinia, realtime = null, shell, vueApp
62
96
  });
63
97
  // A socket refused for an expired session reconnects with the cookies this
64
98
  // session read stores.
65
- releaseBeforeReconnect = realtime?.setBeforeReconnect(() => guard.refresh()) || null;
66
- vueApp?.provide?.(AUTH_GUARD_RUNTIME_INJECTION_KEY, guard);
67
- vueApp?.provide?.(AUTH_OAUTH_LAUNCH_CLIENT_INJECTION_KEY, oauthLaunch);
99
+ releaseBeforeReconnect = realtime.setBeforeReconnect(() => guard.refresh());
100
+ vueApp.provide(AUTH_GUARD_RUNTIME_INJECTION_KEY, guard);
101
+ vueApp.provide(AUTH_OAUTH_LAUNCH_CLIENT_INJECTION_KEY, oauthLaunch);
68
102
  }
69
103
 
70
104
  function dispose() {
71
- stopAuthRefresh?.();
72
- stopAuthRefresh = null;
73
- releaseBeforeReconnect?.();
74
- releaseBeforeReconnect = null;
75
- guard.dispose?.();
105
+ stopAuthRefresh();
106
+ stopAuthRefresh = releaseNothing;
107
+ releaseBeforeReconnect();
108
+ releaseBeforeReconnect = releaseNothing;
109
+ guard.dispose();
76
110
  }
77
111
 
78
112
  return Object.freeze({
@@ -84,4 +118,4 @@ function createAuthClient({ mobile = null, pinia, realtime = null, shell, vueApp
84
118
  });
85
119
  }
86
120
 
87
- export { createAuthClient, createMobileCallbackCompleter };
121
+ export { createAuthClient, createMobileCallbackCompleter, NO_AUTH_REALTIME };
@@ -6,6 +6,7 @@ import { normalizePathname as normalizeSurfacePathname } from "@jskit-ai/kernel/
6
6
  import { createListenerSubscription } from "@jskit-ai/kernel/shared/support/listenerSet";
7
7
  import { normalizePermissionList } from "@jskit-ai/kernel/shared/support/permissions";
8
8
  import { normalizeAuthCapabilities } from "@jskit-ai/auth-core/shared/authCapabilities";
9
+ import { pageDocument, pageLocation, pageWindow } from "../browser.js";
9
10
 
10
11
  const AUTH_POLICY_AUTHENTICATED = "authenticated";
11
12
  const DEFAULT_SESSION_PATH = AUTH_PATHS.SESSION;
@@ -44,12 +45,13 @@ const DEFAULT_AUTH_STATE = Object.freeze({
44
45
  authCapabilities: normalizeAuthCapabilities()
45
46
  });
46
47
 
47
- function asGlobalObject() {
48
- if (typeof globalThis !== "object" || !globalThis) {
49
- return null;
50
- }
51
- return globalThis;
52
- }
48
+ function ignoreRealtimeEvent() {}
49
+
50
+ /** The socket of an application without realtime: no event ever arrives. */
51
+ const NO_REALTIME_REFRESH_SOCKET = Object.freeze({
52
+ on: ignoreRealtimeEvent,
53
+ off: ignoreRealtimeEvent
54
+ });
53
55
 
54
56
  function normalizePathname(pathname, fallback = "/") {
55
57
  const raw = String(pathname || "").trim();
@@ -65,9 +67,7 @@ function normalizeLoginRoute(loginRoute, fallback = DEFAULT_LOGIN_ROUTE) {
65
67
  if (isExternalLinkTarget(raw)) {
66
68
  if (raw.startsWith("//")) {
67
69
  try {
68
- const baseOrigin =
69
- typeof window === "object" && window?.location?.origin ? window.location.origin : "http://localhost";
70
- return new URL(raw, baseOrigin).toString();
70
+ return new URL(raw, pageLocation()?.origin || "http://localhost").toString();
71
71
  } catch {
72
72
  return fallback;
73
73
  }
@@ -94,7 +94,10 @@ function normalizeOAuthProviders(rawProviders) {
94
94
  if (!id) {
95
95
  return null;
96
96
  }
97
- return Object.freeze({ id, label: label || id });
97
+ return Object.freeze({
98
+ id,
99
+ label: label || id
100
+ });
98
101
  })
99
102
  .filter(Boolean);
100
103
 
@@ -123,10 +126,7 @@ function normalizeAuthState(payload = {}) {
123
126
  });
124
127
  }
125
128
 
126
- function isPlacementRuntime(value) {
127
- return Boolean(value && typeof value.getContext === "function" && typeof value.setContext === "function");
128
- }
129
-
129
+ // readable-check: allow method-probe - input validation: the guard API takes the runtime itself or options holding it
130
130
  function isAuthGuardRuntime(value) {
131
131
  return Boolean(
132
132
  value &&
@@ -154,10 +154,6 @@ function applyAuthContext(nextState, placementRuntime) {
154
154
  }
155
155
 
156
156
  async function readSessionState({ sessionPath = DEFAULT_SESSION_PATH, fetchImplementation = globalThis.fetch } = {}) {
157
- if (typeof fetchImplementation !== "function") {
158
- return DEFAULT_AUTH_STATE;
159
- }
160
-
161
157
  try {
162
158
  const response = await fetchImplementation(sessionPath, {
163
159
  method: "GET",
@@ -183,19 +179,14 @@ async function readSessionState({ sessionPath = DEFAULT_SESSION_PATH, fetchImple
183
179
 
184
180
  function resolveReturnToPath(context, { absolute = false } = {}) {
185
181
  if (absolute) {
186
- const href = String(context?.location?.href || (typeof window === "object" ? window.location?.href : "") || "").trim();
182
+ const href = String(context?.location?.href || pageLocation()?.href || "").trim();
187
183
  if (href && isExternalLinkTarget(href)) {
188
184
  return href;
189
185
  }
190
186
  }
191
187
 
192
- const pathname = normalizePathname(
193
- context?.location?.pathname || (typeof window === "object" ? window.location?.pathname : "") || "",
194
- "/"
195
- );
196
- const search = String(
197
- context?.location?.search || (typeof window === "object" ? window.location?.search : "") || ""
198
- ).trim();
188
+ const pathname = normalizePathname(context?.location?.pathname || pageLocation()?.pathname || "", "/");
189
+ const search = String(context?.location?.search || pageLocation()?.search || "").trim();
199
190
  if (!search) {
200
191
  return pathname;
201
192
  }
@@ -208,10 +199,7 @@ function toLoginRedirect(loginRoute, context) {
208
199
  const returnTo = resolveReturnToPath(context, { absolute: externalLoginRoute });
209
200
  if (externalLoginRoute) {
210
201
  try {
211
- const parsed = new URL(
212
- normalizedLoginRoute,
213
- typeof window === "object" && window?.location?.origin ? window.location.origin : "http://localhost"
214
- );
202
+ const parsed = new URL(normalizedLoginRoute, pageLocation()?.origin || "http://localhost");
215
203
  if (returnTo) {
216
204
  parsed.searchParams.set("returnTo", returnTo);
217
205
  }
@@ -260,11 +248,6 @@ function evaluateAuthGuard({ guard, context, loginRoute, authState = DEFAULT_AUT
260
248
  }
261
249
 
262
250
  function installGuardEvaluator({ loginRoute = DEFAULT_LOGIN_ROUTE, getAuthState }) {
263
- const root = asGlobalObject();
264
- if (!root || typeof getAuthState !== "function") {
265
- return () => {};
266
- }
267
-
268
251
  const evaluator = ({ guard, context } = {}) => {
269
252
  return evaluateAuthGuard({
270
253
  guard,
@@ -273,10 +256,10 @@ function installGuardEvaluator({ loginRoute = DEFAULT_LOGIN_ROUTE, getAuthState
273
256
  authState: getAuthState()
274
257
  });
275
258
  };
276
- root[DEFAULT_GUARD_EVALUATOR_KEY] = evaluator;
259
+ globalThis[DEFAULT_GUARD_EVALUATOR_KEY] = evaluator;
277
260
  return () => {
278
- if (root[DEFAULT_GUARD_EVALUATOR_KEY] === evaluator) {
279
- delete root[DEFAULT_GUARD_EVALUATOR_KEY];
261
+ if (globalThis[DEFAULT_GUARD_EVALUATOR_KEY] === evaluator) {
262
+ delete globalThis[DEFAULT_GUARD_EVALUATOR_KEY];
280
263
  }
281
264
  };
282
265
  }
@@ -302,12 +285,8 @@ function stripOAuthCallbackParamsFromSearch(search = "") {
302
285
  }
303
286
 
304
287
  function redirectOAuthCallbackToLoginRoute(loginRoute) {
305
- if (
306
- typeof window !== "object" ||
307
- !window ||
308
- !window.location ||
309
- typeof window.location.replace !== "function"
310
- ) {
288
+ const location = pageLocation();
289
+ if (!location) {
311
290
  return false;
312
291
  }
313
292
 
@@ -316,15 +295,15 @@ function redirectOAuthCallbackToLoginRoute(loginRoute) {
316
295
  return false;
317
296
  }
318
297
 
319
- const currentPathname = normalizePathname(window.location.pathname || "", "/");
298
+ const currentPathname = normalizePathname(location.pathname || "", "/");
320
299
  const loginPathname = normalizePathname(normalizedLoginRoute, DEFAULT_LOGIN_ROUTE);
321
300
  const resetPasswordPathname = normalizePathname(DEFAULT_RESET_PASSWORD_ROUTE, DEFAULT_RESET_PASSWORD_ROUTE);
322
301
  if (currentPathname === loginPathname || currentPathname === resetPasswordPathname) {
323
302
  return false;
324
303
  }
325
304
 
326
- const currentSearch = String(window.location.search || "");
327
- const currentHash = String(window.location.hash || "");
305
+ const currentSearch = String(location.search || "");
306
+ const currentHash = String(location.hash || "");
328
307
  if (!hasOAuthCallbackParams({ search: currentSearch, hash: currentHash })) {
329
308
  return false;
330
309
  }
@@ -337,55 +316,22 @@ function redirectOAuthCallbackToLoginRoute(loginRoute) {
337
316
  nextParams.set(OAUTH_QUERY_PARAM_RETURN_TO, returnTo);
338
317
  const nextQuery = nextParams.toString();
339
318
  const nextPath = `${loginPathname}${nextQuery ? `?${nextQuery}` : ""}${currentHash}`;
340
- window.location.replace(nextPath);
319
+ location.replace(nextPath);
341
320
  return true;
342
321
  }
343
322
 
344
- function asEventTarget(value) {
345
- if (!value || typeof value !== "object") {
346
- return null;
347
- }
348
- if (typeof value.addEventListener !== "function" || typeof value.removeEventListener !== "function") {
349
- return null;
350
- }
351
- return value;
352
- }
353
-
354
- function getWindowEventTarget() {
355
- if (typeof window !== "object" || !window) {
356
- return null;
357
- }
358
- return asEventTarget(window);
359
- }
360
-
361
- function getDocumentEventTarget() {
362
- if (typeof document !== "object" || !document) {
363
- return null;
364
- }
365
- return asEventTarget(document);
366
- }
367
-
368
323
  function isDocumentVisible() {
369
- if (typeof document !== "object" || !document) {
324
+ const documentTarget = pageDocument();
325
+ if (!documentTarget) {
370
326
  return false;
371
327
  }
372
- const visibilityState = String(document.visibilityState || "").trim().toLowerCase();
328
+ const visibilityState = String(documentTarget.visibilityState || "").trim().toLowerCase();
373
329
  if (!visibilityState) {
374
330
  return true;
375
331
  }
376
332
  return visibilityState === "visible";
377
333
  }
378
334
 
379
- function asRealtimeSocket(value) {
380
- if (!value || typeof value !== "object") {
381
- return null;
382
- }
383
- if (typeof value.on !== "function" || typeof value.off !== "function") {
384
- return null;
385
- }
386
- return value;
387
- }
388
-
389
335
  function normalizeRealtimeRefreshEvents(value) {
390
336
  const source = Array.isArray(value) ? value : [value];
391
337
  const deduped = [];
@@ -405,25 +351,33 @@ function normalizeRealtimeRefreshEvents(value) {
405
351
  return Object.freeze(deduped);
406
352
  }
407
353
 
354
+ /**
355
+ * @param {{
356
+ * placementRuntime: import("../types.js").AuthPlacementRuntime,
357
+ * sessionPath?: string,
358
+ * loginRoute?: string,
359
+ * fetchImplementation?: typeof globalThis.fetch,
360
+ * refreshOnForeground?: boolean,
361
+ * refreshOnReconnect?: boolean,
362
+ * realtimeSocket?: import("../types.js").AuthRealtimeSocket,
363
+ * realtimeRefreshEvents?: string | readonly string[]
364
+ * }} options
365
+ * @returns {import("../types.js").AuthGuardRuntime}
366
+ */
408
367
  function createAuthGuardRuntime({
409
- placementRuntime = null,
368
+ placementRuntime,
410
369
  sessionPath = DEFAULT_SESSION_PATH,
411
370
  loginRoute = DEFAULT_LOGIN_ROUTE,
412
371
  fetchImplementation = globalThis.fetch,
413
372
  refreshOnForeground = DEFAULT_REFRESH_ON_FOREGROUND,
414
373
  refreshOnReconnect = DEFAULT_REFRESH_ON_RECONNECT,
415
- realtimeSocket = null,
374
+ realtimeSocket = NO_REALTIME_REFRESH_SOCKET,
416
375
  realtimeRefreshEvents = DEFAULT_REALTIME_REFRESH_EVENTS
417
- } = {}) {
418
- if (!isPlacementRuntime(placementRuntime)) {
419
- throw new Error("createAuthGuardRuntime requires a web placement runtime with getContext()/setContext().");
420
- }
421
-
376
+ }) {
422
377
  let currentSessionPath = normalizeRuntimePath(sessionPath, DEFAULT_SESSION_PATH);
423
378
  let currentLoginRoute = normalizeLoginRoute(loginRoute, DEFAULT_LOGIN_ROUTE);
424
379
  const foregroundRefreshEnabled = refreshOnForeground === true;
425
380
  const reconnectRefreshEnabled = refreshOnReconnect === true;
426
- const socket = asRealtimeSocket(realtimeSocket);
427
381
  const realtimeEvents = normalizeRealtimeRefreshEvents(realtimeRefreshEvents);
428
382
  let authState = DEFAULT_AUTH_STATE;
429
383
  let activeRefreshPromise = null;
@@ -505,7 +459,7 @@ function createAuthGuardRuntime({
505
459
  void refresh();
506
460
  };
507
461
 
508
- const windowTarget = getWindowEventTarget();
462
+ const windowTarget = pageWindow();
509
463
  if (windowTarget) {
510
464
  if (reconnectRefreshEnabled) {
511
465
  windowTarget.addEventListener("online", onReconnect);
@@ -517,17 +471,15 @@ function createAuthGuardRuntime({
517
471
  }
518
472
  }
519
473
 
520
- const documentTarget = getDocumentEventTarget();
474
+ const documentTarget = pageDocument();
521
475
  if (foregroundRefreshEnabled && documentTarget) {
522
476
  documentTarget.addEventListener("visibilitychange", onVisibilityChange);
523
477
  cleanup.push(() => documentTarget.removeEventListener("visibilitychange", onVisibilityChange));
524
478
  }
525
479
 
526
- if (socket) {
527
- for (const eventName of realtimeEvents) {
528
- socket.on(eventName, onRealtimeRefresh);
529
- cleanup.push(() => socket.off(eventName, onRealtimeRefresh));
530
- }
480
+ for (const eventName of realtimeEvents) {
481
+ realtimeSocket.on(eventName, onRealtimeRefresh);
482
+ cleanup.push(() => realtimeSocket.off(eventName, onRealtimeRefresh));
531
483
  }
532
484
  }
533
485
 
@@ -1,66 +1,74 @@
1
1
  import { inject } from "vue";
2
2
  import { normalizeAuthCapabilities } from "@jskit-ai/auth-core/shared/authCapabilities";
3
- import { isAuthGuardRuntime } from "./authGuardRuntime.js";
4
- import { isAuthOAuthLaunchClient } from "./oauthLaunchClient.js";
5
3
  import { createPopupOAuthLaunchClient } from "./popupOAuthLaunchClient.js";
6
4
 
7
5
  const AUTH_GUARD_RUNTIME_INJECTION_KEY = "jskit.auth-web.runtime.auth-guard.client";
8
6
  const AUTH_OAUTH_LAUNCH_CLIENT_INJECTION_KEY = "jskit.auth-web.runtime.oauth-launch.client";
9
7
 
10
- const EMPTY_AUTH_GUARD_STATE = Object.freeze({
8
+ /** @type {import("../types.js").AuthGuardState} */
9
+ const NO_AUTH_GUARD_STATE = Object.freeze({
11
10
  authenticated: false,
12
11
  username: "",
12
+ email: "",
13
+ permissions: Object.freeze([]),
13
14
  oauthDefaultProvider: "",
14
15
  oauthProviders: Object.freeze([]),
15
16
  authCapabilities: normalizeAuthCapabilities()
16
17
  });
17
18
 
18
- const EMPTY_AUTH_GUARD_RUNTIME = Object.freeze({
19
+ function stopListening() {}
20
+
21
+ /**
22
+ * The guard runtime of an application without auth's provider: always signed out.
23
+ * @type {import("../types.js").AuthGuardRuntime}
24
+ */
25
+ const NO_AUTH_GUARD_RUNTIME = Object.freeze({
26
+ dispose() {},
19
27
  async initialize() {
20
- return EMPTY_AUTH_GUARD_STATE;
28
+ return NO_AUTH_GUARD_STATE;
21
29
  },
22
30
  async refresh() {
23
- return EMPTY_AUTH_GUARD_STATE;
31
+ return NO_AUTH_GUARD_STATE;
24
32
  },
25
33
  getState() {
26
- return EMPTY_AUTH_GUARD_STATE;
34
+ return NO_AUTH_GUARD_STATE;
27
35
  },
28
36
  subscribe() {
29
- return () => {};
37
+ return stopListening;
30
38
  }
31
39
  });
32
40
 
33
- function useAuthGuardRuntime({ required = false } = {}) {
34
- const runtime = inject(AUTH_GUARD_RUNTIME_INJECTION_KEY, null);
35
- if (isAuthGuardRuntime(runtime)) {
36
- return runtime;
37
- }
41
+ /** @returns {import("../types.js").AuthGuardRuntime} */
42
+ function useAuthGuardRuntime() {
43
+ return inject(AUTH_GUARD_RUNTIME_INJECTION_KEY, NO_AUTH_GUARD_RUNTIME);
44
+ }
38
45
 
39
- if (required) {
46
+ /**
47
+ * The guard runtime for code that cannot work signed out by default.
48
+ * @returns {import("../types.js").AuthGuardRuntime}
49
+ */
50
+ function useRequiredAuthGuardRuntime() {
51
+ const runtime = useAuthGuardRuntime();
52
+ if (runtime === NO_AUTH_GUARD_RUNTIME) {
40
53
  throw new Error("Auth guard runtime is not available in Vue injection context.");
41
54
  }
42
-
43
- return EMPTY_AUTH_GUARD_RUNTIME;
55
+ return runtime;
44
56
  }
45
57
 
46
- function useAuthOAuthLaunchClient({ required = false } = {}) {
47
- const client = inject(AUTH_OAUTH_LAUNCH_CLIENT_INJECTION_KEY, null);
48
- if (isAuthOAuthLaunchClient(client)) {
49
- return client;
50
- }
51
-
52
- if (required) {
53
- throw new Error("OAuth launch client is not available in Vue injection context.");
54
- }
55
-
56
- return createPopupOAuthLaunchClient();
58
+ /**
59
+ * The application's launch client, or a new popup client where the application provides none.
60
+ * @returns {import("../types.js").AuthOAuthLaunchClient}
61
+ */
62
+ function useAuthOAuthLaunchClient() {
63
+ return inject(AUTH_OAUTH_LAUNCH_CLIENT_INJECTION_KEY, createPopupOAuthLaunchClient, true);
57
64
  }
58
65
 
59
66
  export {
60
67
  AUTH_GUARD_RUNTIME_INJECTION_KEY,
61
68
  AUTH_OAUTH_LAUNCH_CLIENT_INJECTION_KEY,
62
- EMPTY_AUTH_GUARD_STATE,
63
- EMPTY_AUTH_GUARD_RUNTIME,
69
+ NO_AUTH_GUARD_STATE,
70
+ NO_AUTH_GUARD_RUNTIME,
64
71
  useAuthGuardRuntime,
65
- useAuthOAuthLaunchClient
72
+ useAuthOAuthLaunchClient,
73
+ useRequiredAuthGuardRuntime
66
74
  };
@@ -12,8 +12,14 @@ import {
12
12
  } from "./authCallbackUrlParams.js";
13
13
  import { ensureCommandSectionValid } from "../composables/loginView/validationHelpers.js";
14
14
  import { inactiveSessionMessage } from "./inactiveSessionMessage.js";
15
+ import { pageLocation } from "../browser.js";
15
16
  import { useJskitText } from "../i18n.js";
16
17
 
18
+ /** The session of a callback completed without reading it back. */
19
+ async function noRefreshedSession() {
20
+ return null;
21
+ }
22
+
17
23
  // The params of a sign-in error the server explained by redirect (error_params).
18
24
  function readErrorParams(value = "") {
19
25
  try {
@@ -97,7 +103,7 @@ async function completeOAuthCallbackFromUrl({
97
103
  allowedReturnToOrigins = [],
98
104
  defaultProvider = "",
99
105
  request = authHttpRequest,
100
- refreshSession = async () => null
106
+ refreshSession = noRefreshedSession
101
107
  } = {}) {
102
108
  const callbackParams = readOAuthCallbackParamsFromUrl(url);
103
109
  if (!callbackParams) {
@@ -189,19 +195,9 @@ async function completeOAuthCallbackFromUrl({
189
195
  }
190
196
 
191
197
  async function completeOAuthCallbackFromCurrentLocation(options = {}) {
192
- if (typeof window !== "object" || !window?.location) {
193
- return Object.freeze({
194
- handled: false,
195
- completed: false,
196
- returnTo: normalizeAuthReturnToPath("", options.fallbackReturnTo || "/", {
197
- allowedOrigins: options.allowedReturnToOrigins
198
- })
199
- });
200
- }
201
-
202
198
  return completeOAuthCallbackFromUrl({
203
199
  ...options,
204
- url: window.location.href
200
+ url: pageLocation()?.href
205
201
  });
206
202
  }
207
203
 
@@ -1,28 +1,23 @@
1
- function createBrowserOAuthLaunchClient({ location = null } = {}) {
2
- const resolvedLocation =
3
- location || (typeof window === "object" && window?.location ? window.location : null);
1
+ import { pageLocation } from "../browser.js";
4
2
 
5
- return Object.freeze({
6
- async open({ url = "" } = {}) {
7
- const normalizedUrl = String(url || "").trim();
8
- if (!normalizedUrl) {
9
- throw new Error("OAuth launch URL is required.");
10
- }
11
- if (!resolvedLocation || typeof resolvedLocation.assign !== "function") {
12
- throw new Error("Browser location.assign() is unavailable for OAuth launch.");
13
- }
14
-
15
- resolvedLocation.assign(normalizedUrl);
16
- return true;
3
+ /** @param {{ location?: Pick<Location, "assign"> | null }} [options] */
4
+ function createBrowserOAuthLaunchClient({ location = pageLocation() } = {}) {
5
+ async function open({ url = "" } = {}) {
6
+ const normalizedUrl = String(url || "").trim();
7
+ if (!normalizedUrl) {
8
+ throw new Error("OAuth launch URL is required.");
17
9
  }
18
- });
19
- }
10
+ if (!location) {
11
+ throw new Error("Browser location.assign() is unavailable for OAuth launch.");
12
+ }
13
+
14
+ location.assign(normalizedUrl);
15
+ return true;
16
+ }
20
17
 
21
- function isAuthOAuthLaunchClient(value = null) {
22
- return Boolean(value && typeof value === "object" && typeof value.open === "function");
18
+ return Object.freeze({
19
+ open
20
+ });
23
21
  }
24
22
 
25
- export {
26
- createBrowserOAuthLaunchClient,
27
- isAuthOAuthLaunchClient
28
- };
23
+ export { createBrowserOAuthLaunchClient };
@@ -3,6 +3,7 @@ import {
3
3
  readAuthCallbackUrlParams,
4
4
  stripAuthCallbackParamsFromUrl
5
5
  } from "./authCallbackUrlParams.js";
6
+ import { pageWindow } from "../browser.js";
6
7
 
7
8
  const PASSWORD_RECOVERY_CALLBACK_PARAM_KEYS = Object.freeze([
8
9
  "token",
@@ -63,14 +64,15 @@ function stripPasswordRecoveryCallbackParamsFromUrl(url = "") {
63
64
  }
64
65
 
65
66
  function stripPasswordRecoveryCallbackParamsFromLocation() {
66
- if (typeof window !== "object" || !window.location || !window.history) {
67
+ const page = pageWindow();
68
+ if (!page) {
67
69
  return;
68
70
  }
69
71
 
70
- window.history.replaceState(
72
+ page.history.replaceState(
71
73
  {},
72
74
  "",
73
- stripPasswordRecoveryCallbackParamsFromUrl(window.location.href)
75
+ stripPasswordRecoveryCallbackParamsFromUrl(page.location.href)
74
76
  );
75
77
  }
76
78