@bagelink/auth 1.15.519 → 1.15.523

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.cjs CHANGED
@@ -1030,6 +1030,16 @@ let tenancyEnabled = false;
1030
1030
  const accountInfo = vue.ref(null);
1031
1031
  const tenants = vue.ref([]);
1032
1032
  const currentTenant = vue.ref(null);
1033
+ let initialAuthCheck = null;
1034
+ function ensureInitialAuth() {
1035
+ if (initialAuthCheck === null) {
1036
+ initialAuthCheck = useAuth().checkAuth().catch(() => false);
1037
+ }
1038
+ return initialAuthCheck;
1039
+ }
1040
+ function resetInitialAuth() {
1041
+ initialAuthCheck = null;
1042
+ }
1033
1043
  function getRedirectConfig() {
1034
1044
  if (!redirectConfig) {
1035
1045
  throw new Error("Redirect config not initialized. Did you call createAuth with redirect config?");
@@ -1132,6 +1142,9 @@ function createAuth(params) {
1132
1142
  */
1133
1143
  install(app) {
1134
1144
  app.config.globalProperties.$auth = useAuth();
1145
+ },
1146
+ get ready() {
1147
+ return ensureInitialAuth();
1135
1148
  }
1136
1149
  };
1137
1150
  return authInstance;
@@ -1284,7 +1297,7 @@ function useAuth() {
1284
1297
  });
1285
1298
  }
1286
1299
  }
1287
- return true;
1300
+ return getIsLoggedIn();
1288
1301
  } catch {
1289
1302
  accountInfo.value = null;
1290
1303
  return false;
@@ -1554,12 +1567,13 @@ function useAuth() {
1554
1567
  const useAuth$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
1555
1568
  __proto__: null,
1556
1569
  createAuth,
1570
+ ensureInitialAuth,
1557
1571
  getRedirectConfig,
1572
+ resetInitialAuth,
1558
1573
  useAuth
1559
1574
  }, Symbol.toStringTag, { value: "Module" }));
1560
- let authInitialized = false;
1561
1575
  function resetAuthState() {
1562
- authInitialized = false;
1576
+ resetInitialAuth();
1563
1577
  }
1564
1578
  function authGuard() {
1565
1579
  return async function guard(to, _from) {
@@ -1569,9 +1583,8 @@ function authGuard() {
1569
1583
  const requiresNoAuth = config.noAuthRoutes.includes(to.name);
1570
1584
  const skipsAuthBootstrap = to.meta.skipAuthBootstrap === true;
1571
1585
  try {
1572
- if (!authInitialized && !skipsAuthBootstrap) {
1573
- await auth.checkAuth();
1574
- authInitialized = true;
1586
+ if (!skipsAuthBootstrap) {
1587
+ await ensureInitialAuth();
1575
1588
  }
1576
1589
  const isAuthenticated = !!auth.user.value;
1577
1590
  if (isAuthenticated && requiresNoAuth) {
@@ -1635,6 +1648,7 @@ exports.buildLoginQuery = buildLoginQuery;
1635
1648
  exports.composeGuards = composeGuards;
1636
1649
  exports.createAuth = createAuth;
1637
1650
  exports.createAuthGuard = createAuthGuard;
1651
+ exports.ensureInitialAuth = ensureInitialAuth;
1638
1652
  exports.getAllSSOProviders = getAllSSOProviders;
1639
1653
  exports.getRedirectConfig = getRedirectConfig;
1640
1654
  exports.getRedirectUrl = getRedirectUrl;
@@ -1646,6 +1660,7 @@ exports.normalizeRedirectConfig = normalizeRedirectConfig;
1646
1660
  exports.performRedirect = performRedirect;
1647
1661
  exports.providers = providers;
1648
1662
  exports.resetAuthState = resetAuthState;
1663
+ exports.resetInitialAuth = resetInitialAuth;
1649
1664
  exports.setAuthContext = setAuthContext;
1650
1665
  exports.sso = sso;
1651
1666
  exports.ssoProvidersList = ssoProvidersList;
package/dist/index.mjs CHANGED
@@ -1028,6 +1028,16 @@ let tenancyEnabled = false;
1028
1028
  const accountInfo = ref(null);
1029
1029
  const tenants = ref([]);
1030
1030
  const currentTenant = ref(null);
1031
+ let initialAuthCheck = null;
1032
+ function ensureInitialAuth() {
1033
+ if (initialAuthCheck === null) {
1034
+ initialAuthCheck = useAuth().checkAuth().catch(() => false);
1035
+ }
1036
+ return initialAuthCheck;
1037
+ }
1038
+ function resetInitialAuth() {
1039
+ initialAuthCheck = null;
1040
+ }
1031
1041
  function getRedirectConfig() {
1032
1042
  if (!redirectConfig) {
1033
1043
  throw new Error("Redirect config not initialized. Did you call createAuth with redirect config?");
@@ -1130,6 +1140,9 @@ function createAuth(params) {
1130
1140
  */
1131
1141
  install(app) {
1132
1142
  app.config.globalProperties.$auth = useAuth();
1143
+ },
1144
+ get ready() {
1145
+ return ensureInitialAuth();
1133
1146
  }
1134
1147
  };
1135
1148
  return authInstance;
@@ -1282,7 +1295,7 @@ function useAuth() {
1282
1295
  });
1283
1296
  }
1284
1297
  }
1285
- return true;
1298
+ return getIsLoggedIn();
1286
1299
  } catch {
1287
1300
  accountInfo.value = null;
1288
1301
  return false;
@@ -1552,12 +1565,13 @@ function useAuth() {
1552
1565
  const useAuth$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
1553
1566
  __proto__: null,
1554
1567
  createAuth,
1568
+ ensureInitialAuth,
1555
1569
  getRedirectConfig,
1570
+ resetInitialAuth,
1556
1571
  useAuth
1557
1572
  }, Symbol.toStringTag, { value: "Module" }));
1558
- let authInitialized = false;
1559
1573
  function resetAuthState() {
1560
- authInitialized = false;
1574
+ resetInitialAuth();
1561
1575
  }
1562
1576
  function authGuard() {
1563
1577
  return async function guard(to, _from) {
@@ -1567,9 +1581,8 @@ function authGuard() {
1567
1581
  const requiresNoAuth = config.noAuthRoutes.includes(to.name);
1568
1582
  const skipsAuthBootstrap = to.meta.skipAuthBootstrap === true;
1569
1583
  try {
1570
- if (!authInitialized && !skipsAuthBootstrap) {
1571
- await auth.checkAuth();
1572
- authInitialized = true;
1584
+ if (!skipsAuthBootstrap) {
1585
+ await ensureInitialAuth();
1573
1586
  }
1574
1587
  const isAuthenticated = !!auth.user.value;
1575
1588
  if (isAuthenticated && requiresNoAuth) {
@@ -1634,6 +1647,7 @@ export {
1634
1647
  composeGuards,
1635
1648
  createAuth,
1636
1649
  createAuthGuard,
1650
+ ensureInitialAuth,
1637
1651
  getAllSSOProviders,
1638
1652
  getRedirectConfig,
1639
1653
  getRedirectUrl,
@@ -1645,6 +1659,7 @@ export {
1645
1659
  performRedirect,
1646
1660
  providers,
1647
1661
  resetAuthState,
1662
+ resetInitialAuth,
1648
1663
  setAuthContext,
1649
1664
  sso,
1650
1665
  ssoProvidersList,
package/dist/router.d.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  import { NavigationGuard, RouteLocationNormalized, RouteLocationRaw } from 'vue-router';
2
+ /**
3
+ * Auth router integration for Vue Router
4
+ */
2
5
  /**
3
6
  * Reset auth initialization state
4
7
  * Useful for testing or app reload scenarios
package/dist/useAuth.d.ts CHANGED
@@ -27,7 +27,24 @@ export interface AuthInstance extends ObjectPlugin<[]> {
27
27
  }) => AuthInstance;
28
28
  routerGuard: () => (to: any, from: any) => Promise<any>;
29
29
  install: (app: App) => void;
30
+ /**
31
+ * Resolves once the initial session check (`/me`) has completed — logged in
32
+ * or not. Never rejects. Await before `app.mount()` so nothing renders
33
+ * before the auth state is known:
34
+ * ```ts
35
+ * await auth.ready
36
+ * app.mount('#app')
37
+ * ```
38
+ */
39
+ readonly ready: Promise<boolean>;
30
40
  }
41
+ /**
42
+ * Run (once) and return the initial session check. Resolves `true` when a user
43
+ * is logged in, `false` otherwise (a 401 from `/me` just means "logged out").
44
+ */
45
+ export declare function ensureInitialAuth(): Promise<boolean>;
46
+ /** Forget the initial check (tests / full re-init). */
47
+ export declare function resetInitialAuth(): void;
31
48
  /**
32
49
  * Get the current redirect configuration
33
50
  * Used internally by router guard
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bagelink/auth",
3
3
  "type": "module",
4
- "version": "1.15.519",
4
+ "version": "1.15.523",
5
5
  "description": "Bagelink auth package",
6
6
  "author": {
7
7
  "name": "Bagel Studio",
package/src/router.ts CHANGED
@@ -1,20 +1,17 @@
1
1
  import type { NavigationGuard, RouteLocationNormalized, RouteLocationRaw } from 'vue-router'
2
2
  import { buildLoginQuery } from './redirect'
3
- import { useAuth, getRedirectConfig } from './useAuth'
3
+ import { useAuth, getRedirectConfig, ensureInitialAuth, resetInitialAuth } from './useAuth'
4
4
 
5
5
  /**
6
6
  * Auth router integration for Vue Router
7
7
  */
8
8
 
9
- // Track if auth has been initialized (for performance)
10
- let authInitialized = false
11
-
12
9
  /**
13
10
  * Reset auth initialization state
14
11
  * Useful for testing or app reload scenarios
15
12
  */
16
13
  export function resetAuthState() {
17
- authInitialized = false
14
+ resetInitialAuth()
18
15
  }
19
16
 
20
17
  /**
@@ -48,10 +45,10 @@ export function authGuard() {
48
45
  const skipsAuthBootstrap = to.meta.skipAuthBootstrap === true
49
46
 
50
47
  try {
51
- // Only check auth once on first navigation for performance
52
- if (!authInitialized && !skipsAuthBootstrap) {
53
- await auth.checkAuth()
54
- authInitialized = true
48
+ // Resolve the session once, before the first route renders. Shared
49
+ // with `auth.ready`, so `/me` is only requested once on boot.
50
+ if (!skipsAuthBootstrap) {
51
+ await ensureInitialAuth()
55
52
  }
56
53
 
57
54
  // Use cached auth state from reactive user ref
package/src/useAuth.ts CHANGED
@@ -71,6 +71,36 @@ export interface AuthInstance extends ObjectPlugin<[]> {
71
71
  use: (dependency: any, options?: { guard?: boolean }) => AuthInstance
72
72
  routerGuard: () => (to: any, from: any) => Promise<any>
73
73
  install: (app: App) => void
74
+ /**
75
+ * Resolves once the initial session check (`/me`) has completed — logged in
76
+ * or not. Never rejects. Await before `app.mount()` so nothing renders
77
+ * before the auth state is known:
78
+ * ```ts
79
+ * await auth.ready
80
+ * app.mount('#app')
81
+ * ```
82
+ */
83
+ readonly ready: Promise<boolean>
84
+ }
85
+
86
+ // Single shared initial session check. The router guard and `auth.ready`
87
+ // both await the same promise, so `/me` is requested exactly once on boot.
88
+ let initialAuthCheck: Promise<boolean> | null = null
89
+
90
+ /**
91
+ * Run (once) and return the initial session check. Resolves `true` when a user
92
+ * is logged in, `false` otherwise (a 401 from `/me` just means "logged out").
93
+ */
94
+ export function ensureInitialAuth(): Promise<boolean> {
95
+ if (initialAuthCheck === null) {
96
+ initialAuthCheck = useAuth().checkAuth().catch(() => false)
97
+ }
98
+ return initialAuthCheck
99
+ }
100
+
101
+ /** Forget the initial check (tests / full re-init). */
102
+ export function resetInitialAuth(): void {
103
+ initialAuthCheck = null
74
104
  }
75
105
 
76
106
  /**
@@ -200,6 +230,10 @@ export function createAuth(params: InitParams): AuthInstance {
200
230
  // Make auth available globally
201
231
  app.config.globalProperties.$auth = useAuth()
202
232
  },
233
+
234
+ get ready(): Promise<boolean> {
235
+ return ensureInitialAuth()
236
+ },
203
237
  }
204
238
 
205
239
  return authInstance
@@ -414,6 +448,10 @@ export function useAuth() {
414
448
  }
415
449
  }
416
450
 
451
+ /**
452
+ * Resolve the current session. Never throws: a 401 from `/me` simply means
453
+ * "logged out" and is not an error. Returns whether a user is logged in.
454
+ */
417
455
  async function checkAuth(): Promise<boolean> {
418
456
  try {
419
457
  const { data } = await api.getCurrentUser()
@@ -424,7 +462,7 @@ export function useAuth() {
424
462
  await loadTenants().catch(() => {})
425
463
  }
426
464
  }
427
- return true
465
+ return getIsLoggedIn()
428
466
  } catch {
429
467
  accountInfo.value = null
430
468
  return false