@bagelink/auth 1.12.23 → 1.12.27

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
@@ -1041,11 +1041,11 @@ function createAuth(params) {
1041
1041
  *
1042
1042
  * // Manual guard control (for custom composition)
1043
1043
  * auth.use(router, { guard: false })
1044
- * router.beforeEach(async (to, from, next) => {
1044
+ * router.beforeEach(async (to, from) => {
1045
1045
  * // Custom logic first
1046
- * if (!hasOrgAccess(to)) return next('/no-access')
1046
+ * if (!hasOrgAccess(to)) return '/no-access'
1047
1047
  * // Then run auth guard
1048
- * return auth.routerGuard()(to, from, next)
1048
+ * return auth.routerGuard()(to, from)
1049
1049
  * })
1050
1050
  * ```
1051
1051
  */
@@ -1074,11 +1074,11 @@ function createAuth(params) {
1074
1074
  */
1075
1075
  routerGuard() {
1076
1076
  if (cachedAuthGuard === null) {
1077
- cachedAuthGuard = async (to, from, next) => {
1077
+ cachedAuthGuard = async (to, from) => {
1078
1078
  const { authGuard: authGuard2 } = await Promise.resolve().then(() => router);
1079
1079
  const guard = authGuard2();
1080
1080
  cachedAuthGuard = guard;
1081
- return guard(to, from, next);
1081
+ return guard(to, from);
1082
1082
  };
1083
1083
  }
1084
1084
  return cachedAuthGuard;
@@ -1255,13 +1255,10 @@ function useAuth() {
1255
1255
  try {
1256
1256
  const { data } = await api.getTenants();
1257
1257
  tenants.value = data;
1258
- console.log("[Auth] Loaded tenants:", tenants.value);
1259
1258
  if (currentTenant.value === null && tenants.value.length > 0) {
1260
1259
  const firstActiveTenant = tenants.value.find((t) => t.status === "active");
1261
1260
  if (firstActiveTenant !== void 0) {
1262
- console.log("[Auth] Auto-selecting tenant:", firstActiveTenant.id);
1263
1261
  setTenant(firstActiveTenant.id);
1264
- console.log("[Auth] Tenant set. Current tenant ID in API:", api.getTenantId());
1265
1262
  }
1266
1263
  }
1267
1264
  return tenants.value;
@@ -1524,7 +1521,7 @@ function resetAuthState() {
1524
1521
  authInitialized = false;
1525
1522
  }
1526
1523
  function authGuard() {
1527
- return async function guard(to, _from, next) {
1524
+ return async function guard(to, _from) {
1528
1525
  const auth = useAuth();
1529
1526
  const config = getRedirectConfig();
1530
1527
  const requiresAuth = to.meta[config.authMetaKey];
@@ -1536,43 +1533,31 @@ function authGuard() {
1536
1533
  }
1537
1534
  const isAuthenticated = !!auth.user.value;
1538
1535
  if (isAuthenticated && requiresNoAuth) {
1539
- next(config.authenticatedRedirect);
1540
- return;
1536
+ return config.authenticatedRedirect;
1541
1537
  }
1542
1538
  if (!isAuthenticated && requiresAuth) {
1543
1539
  const query = buildLoginQuery(to.fullPath, config);
1544
- next({
1540
+ return {
1545
1541
  name: config.loginRoute,
1546
1542
  query
1547
- });
1548
- return;
1543
+ };
1549
1544
  }
1550
- next();
1545
+ return true;
1551
1546
  } catch (error) {
1552
1547
  console.error("[Auth Guard] Error:", error);
1553
- next();
1548
+ return true;
1554
1549
  }
1555
1550
  };
1556
1551
  }
1557
1552
  function composeGuards(guards) {
1558
- return async (to, from, next) => {
1559
- let guardIndex = 0;
1560
- const runNextGuard = async () => {
1561
- if (guardIndex >= guards.length) {
1562
- next();
1563
- return;
1553
+ return async (to, from) => {
1554
+ for (const guard of guards) {
1555
+ const result = await guard(to, from);
1556
+ if (result !== void 0 && result !== true) {
1557
+ return result;
1564
1558
  }
1565
- const guard = guards[guardIndex];
1566
- guardIndex++;
1567
- await guard(to, from, (result) => {
1568
- if (result !== void 0) {
1569
- next(result);
1570
- } else {
1571
- runNextGuard();
1572
- }
1573
- });
1574
- };
1575
- await runNextGuard();
1559
+ }
1560
+ return true;
1576
1561
  };
1577
1562
  }
1578
1563
  const router = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
@@ -1583,14 +1568,13 @@ const router = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProper
1583
1568
  }, Symbol.toStringTag, { value: "Module" }));
1584
1569
  function createAuthGuard(config = {}) {
1585
1570
  const { redirectTo = "/" } = config;
1586
- return async (to, _from, next) => {
1571
+ return async (to, _) => {
1587
1572
  const { useAuth: useAuth2 } = await Promise.resolve().then(() => useAuth$1);
1588
1573
  const { user } = useAuth2();
1589
1574
  if (to.meta.requiresAuth === false && user.value) {
1590
- next(redirectTo);
1591
- } else {
1592
- next();
1575
+ return redirectTo;
1593
1576
  }
1577
+ return true;
1594
1578
  };
1595
1579
  }
1596
1580
  exports.AuthApi = AuthApi;
package/dist/index.mjs CHANGED
@@ -1039,11 +1039,11 @@ function createAuth(params) {
1039
1039
  *
1040
1040
  * // Manual guard control (for custom composition)
1041
1041
  * auth.use(router, { guard: false })
1042
- * router.beforeEach(async (to, from, next) => {
1042
+ * router.beforeEach(async (to, from) => {
1043
1043
  * // Custom logic first
1044
- * if (!hasOrgAccess(to)) return next('/no-access')
1044
+ * if (!hasOrgAccess(to)) return '/no-access'
1045
1045
  * // Then run auth guard
1046
- * return auth.routerGuard()(to, from, next)
1046
+ * return auth.routerGuard()(to, from)
1047
1047
  * })
1048
1048
  * ```
1049
1049
  */
@@ -1072,11 +1072,11 @@ function createAuth(params) {
1072
1072
  */
1073
1073
  routerGuard() {
1074
1074
  if (cachedAuthGuard === null) {
1075
- cachedAuthGuard = async (to, from, next) => {
1075
+ cachedAuthGuard = async (to, from) => {
1076
1076
  const { authGuard: authGuard2 } = await Promise.resolve().then(() => router);
1077
1077
  const guard = authGuard2();
1078
1078
  cachedAuthGuard = guard;
1079
- return guard(to, from, next);
1079
+ return guard(to, from);
1080
1080
  };
1081
1081
  }
1082
1082
  return cachedAuthGuard;
@@ -1253,13 +1253,10 @@ function useAuth() {
1253
1253
  try {
1254
1254
  const { data } = await api.getTenants();
1255
1255
  tenants.value = data;
1256
- console.log("[Auth] Loaded tenants:", tenants.value);
1257
1256
  if (currentTenant.value === null && tenants.value.length > 0) {
1258
1257
  const firstActiveTenant = tenants.value.find((t) => t.status === "active");
1259
1258
  if (firstActiveTenant !== void 0) {
1260
- console.log("[Auth] Auto-selecting tenant:", firstActiveTenant.id);
1261
1259
  setTenant(firstActiveTenant.id);
1262
- console.log("[Auth] Tenant set. Current tenant ID in API:", api.getTenantId());
1263
1260
  }
1264
1261
  }
1265
1262
  return tenants.value;
@@ -1522,7 +1519,7 @@ function resetAuthState() {
1522
1519
  authInitialized = false;
1523
1520
  }
1524
1521
  function authGuard() {
1525
- return async function guard(to, _from, next) {
1522
+ return async function guard(to, _from) {
1526
1523
  const auth = useAuth();
1527
1524
  const config = getRedirectConfig();
1528
1525
  const requiresAuth = to.meta[config.authMetaKey];
@@ -1534,43 +1531,31 @@ function authGuard() {
1534
1531
  }
1535
1532
  const isAuthenticated = !!auth.user.value;
1536
1533
  if (isAuthenticated && requiresNoAuth) {
1537
- next(config.authenticatedRedirect);
1538
- return;
1534
+ return config.authenticatedRedirect;
1539
1535
  }
1540
1536
  if (!isAuthenticated && requiresAuth) {
1541
1537
  const query = buildLoginQuery(to.fullPath, config);
1542
- next({
1538
+ return {
1543
1539
  name: config.loginRoute,
1544
1540
  query
1545
- });
1546
- return;
1541
+ };
1547
1542
  }
1548
- next();
1543
+ return true;
1549
1544
  } catch (error) {
1550
1545
  console.error("[Auth Guard] Error:", error);
1551
- next();
1546
+ return true;
1552
1547
  }
1553
1548
  };
1554
1549
  }
1555
1550
  function composeGuards(guards) {
1556
- return async (to, from, next) => {
1557
- let guardIndex = 0;
1558
- const runNextGuard = async () => {
1559
- if (guardIndex >= guards.length) {
1560
- next();
1561
- return;
1551
+ return async (to, from) => {
1552
+ for (const guard of guards) {
1553
+ const result = await guard(to, from);
1554
+ if (result !== void 0 && result !== true) {
1555
+ return result;
1562
1556
  }
1563
- const guard = guards[guardIndex];
1564
- guardIndex++;
1565
- await guard(to, from, (result) => {
1566
- if (result !== void 0) {
1567
- next(result);
1568
- } else {
1569
- runNextGuard();
1570
- }
1571
- });
1572
- };
1573
- await runNextGuard();
1557
+ }
1558
+ return true;
1574
1559
  };
1575
1560
  }
1576
1561
  const router = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
@@ -1581,14 +1566,13 @@ const router = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProper
1581
1566
  }, Symbol.toStringTag, { value: "Module" }));
1582
1567
  function createAuthGuard(config = {}) {
1583
1568
  const { redirectTo = "/" } = config;
1584
- return async (to, _from, next) => {
1569
+ return async (to, _) => {
1585
1570
  const { useAuth: useAuth2 } = await Promise.resolve().then(() => useAuth$1);
1586
1571
  const { user } = useAuth2();
1587
1572
  if (to.meta.requiresAuth === false && user.value) {
1588
- next(redirectTo);
1589
- } else {
1590
- next();
1573
+ return redirectTo;
1591
1574
  }
1575
+ return true;
1592
1576
  };
1593
1577
  }
1594
1578
  export {
package/dist/router.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { NavigationGuard, NavigationGuardNext, RouteLocationNormalized } from 'vue-router';
1
+ import { NavigationGuard, RouteLocationNormalized, RouteLocationRaw } from 'vue-router';
2
2
  /**
3
3
  * Reset auth initialization state
4
4
  * Useful for testing or app reload scenarios
@@ -19,10 +19,10 @@ export declare function resetAuthState(): void;
19
19
  * router.beforeEach(authGuard())
20
20
  * ```
21
21
  */
22
- export declare function authGuard(): (to: RouteLocationNormalized, _from: RouteLocationNormalized, next: NavigationGuardNext) => Promise<void>;
22
+ export declare function authGuard(): (to: RouteLocationNormalized, _from: RouteLocationNormalized) => Promise<RouteLocationRaw | boolean | void>;
23
23
  /**
24
24
  * Compose multiple navigation guards into one
25
- * Guards are executed in order, stopping at the first one that calls next with a value
25
+ * Guards are executed in order, stopping at the first one that returns a redirect
26
26
  *
27
27
  * @example
28
28
  * ```ts
package/dist/routes.d.ts CHANGED
@@ -10,4 +10,4 @@
10
10
  */
11
11
  export declare function createAuthGuard(config?: {
12
12
  redirectTo?: string;
13
- }): (to: any, _from: any, next: any) => Promise<void>;
13
+ }): (to: any, _: any) => Promise<string | true>;
package/dist/useAuth.d.ts CHANGED
@@ -25,7 +25,7 @@ export interface AuthInstance extends ObjectPlugin<[]> {
25
25
  use: (dependency: any, options?: {
26
26
  guard?: boolean;
27
27
  }) => AuthInstance;
28
- routerGuard: () => (to: any, from: any, next: any) => Promise<void>;
28
+ routerGuard: () => (to: any, from: any) => Promise<any>;
29
29
  install: (app: App) => void;
30
30
  }
31
31
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bagelink/auth",
3
3
  "type": "module",
4
- "version": "1.12.23",
4
+ "version": "1.12.27",
5
5
  "description": "Bagelink auth package",
6
6
  "author": {
7
7
  "name": "Bagel Studio",
package/src/router.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { NavigationGuard, NavigationGuardNext, RouteLocationNormalized } from 'vue-router'
1
+ import type { NavigationGuard, RouteLocationNormalized, RouteLocationRaw } from 'vue-router'
2
2
  import { buildLoginQuery } from './redirect'
3
3
  import { useAuth, getRedirectConfig } from './useAuth'
4
4
 
@@ -34,11 +34,9 @@ export function resetAuthState() {
34
34
  */
35
35
  export function authGuard() {
36
36
  return async function guard(
37
-
38
37
  to: RouteLocationNormalized,
39
38
  _from: RouteLocationNormalized,
40
- next: NavigationGuardNext,
41
- ): Promise<void> {
39
+ ): Promise<RouteLocationRaw | boolean | void> {
42
40
  const auth = useAuth()
43
41
  const config = getRedirectConfig()
44
42
 
@@ -60,34 +58,32 @@ export function authGuard() {
60
58
 
61
59
  // Redirect authenticated users away from auth pages
62
60
  if (isAuthenticated && requiresNoAuth) {
63
- next(config.authenticatedRedirect)
64
- return
61
+ return config.authenticatedRedirect
65
62
  }
66
63
 
67
64
  // Redirect unauthenticated users to login for protected pages
68
65
  if (!isAuthenticated && requiresAuth) {
69
66
  const query = buildLoginQuery(to.fullPath, config)
70
67
 
71
- next({
68
+ return {
72
69
  name: config.loginRoute,
73
70
  query,
74
- })
75
- return
71
+ }
76
72
  }
77
73
 
78
74
  // Allow navigation
79
- next()
75
+ return true
80
76
  } catch (error) {
81
77
  console.error('[Auth Guard] Error:', error)
82
78
  // On error, allow navigation but log the issue
83
- next()
79
+ return true
84
80
  }
85
81
  }
86
82
  }
87
83
 
88
84
  /**
89
85
  * Compose multiple navigation guards into one
90
- * Guards are executed in order, stopping at the first one that calls next with a value
86
+ * Guards are executed in order, stopping at the first one that returns a redirect
91
87
  *
92
88
  * @example
93
89
  * ```ts
@@ -99,31 +95,15 @@ export function authGuard() {
99
95
  * ```
100
96
  */
101
97
  export function composeGuards(guards: NavigationGuard[]): NavigationGuard {
102
- return async (to, from, next) => {
103
- let guardIndex = 0
104
-
105
- const runNextGuard = async (): Promise<void> => {
106
- if (guardIndex >= guards.length) {
107
- // All guards passed, allow navigation
108
- next()
109
- return
98
+ return async (to, from) => {
99
+ for (const guard of guards) {
100
+ const result = await (guard as (to: RouteLocationNormalized, from: RouteLocationNormalized) => Promise<RouteLocationRaw | boolean | void>)(to, from)
101
+ if (result !== undefined && result !== true) {
102
+ // Guard blocked or redirected, stop here
103
+ return result
110
104
  }
111
-
112
- const guard = guards[guardIndex]
113
- guardIndex++
114
-
115
- // Run the current guard
116
- await guard(to, from, (result?: any) => {
117
- if (result !== undefined) {
118
- // Guard blocked or redirected, stop here
119
- next(result)
120
- } else {
121
- // Guard passed, run next guard
122
- runNextGuard()
123
- }
124
- })
125
105
  }
126
-
127
- await runNextGuard()
106
+ // All guards passed, allow navigation
107
+ return true
128
108
  }
129
109
  }
package/src/routes.ts CHANGED
@@ -11,16 +11,15 @@
11
11
  export function createAuthGuard(config: { redirectTo?: string } = {}) {
12
12
  const { redirectTo = '/' } = config
13
13
 
14
- return async (to: any, _from: any, next: any) => {
14
+ return async (to: any, _: any) => {
15
15
  // Import dynamically to avoid circular dependencies
16
16
  const { useAuth } = await import('./useAuth')
17
17
  const { user } = useAuth()
18
18
 
19
19
  // If route doesn't require auth and user is authenticated, redirect
20
20
  if (to.meta.requiresAuth === false && user.value) {
21
- next(redirectTo)
22
- } else {
23
- next()
21
+ return redirectTo
24
22
  }
23
+ return true
25
24
  }
26
25
  }
package/src/useAuth.ts CHANGED
@@ -69,7 +69,7 @@ export interface AuthInstance extends ObjectPlugin<[]> {
69
69
  off: <K extends AuthState>(event: K, handler: AuthEventMap[K]) => void
70
70
  removeAllListeners: <K extends AuthState>(event?: K) => void
71
71
  use: (dependency: any, options?: { guard?: boolean }) => AuthInstance
72
- routerGuard: () => (to: any, from: any, next: any) => Promise<void>
72
+ routerGuard: () => (to: any, from: any) => Promise<any>
73
73
  install: (app: App) => void
74
74
  }
75
75
 
@@ -139,11 +139,11 @@ export function createAuth(params: InitParams): AuthInstance {
139
139
  *
140
140
  * // Manual guard control (for custom composition)
141
141
  * auth.use(router, { guard: false })
142
- * router.beforeEach(async (to, from, next) => {
142
+ * router.beforeEach(async (to, from) => {
143
143
  * // Custom logic first
144
- * if (!hasOrgAccess(to)) return next('/no-access')
144
+ * if (!hasOrgAccess(to)) return '/no-access'
145
145
  * // Then run auth guard
146
- * return auth.routerGuard()(to, from, next)
146
+ * return auth.routerGuard()(to, from)
147
147
  * })
148
148
  * ```
149
149
  */
@@ -177,12 +177,12 @@ export function createAuth(params: InitParams): AuthInstance {
177
177
  routerGuard() {
178
178
  // Return factory that lazily loads authGuard to avoid circular dependency
179
179
  if (cachedAuthGuard === null) {
180
- cachedAuthGuard = async (to: any, from: any, next: any) => {
180
+ cachedAuthGuard = async (to: any, from: any) => {
181
181
  const { authGuard } = await import('./router')
182
182
  const guard = authGuard()
183
183
  // Cache the actual guard for next time
184
184
  cachedAuthGuard = guard
185
- return guard(to, from, next)
185
+ return guard(to, from)
186
186
  }
187
187
  }
188
188
  return cachedAuthGuard
@@ -436,15 +436,11 @@ export function useAuth() {
436
436
  const { data } = await api.getTenants()
437
437
  tenants.value = data
438
438
 
439
- console.log('[Auth] Loaded tenants:', tenants.value)
440
-
441
439
  // Auto-select first tenant if none selected and tenants available
442
440
  if (currentTenant.value === null && tenants.value.length > 0) {
443
441
  const firstActiveTenant = tenants.value.find(t => t.status === 'active')
444
442
  if (firstActiveTenant !== undefined) {
445
- console.log('[Auth] Auto-selecting tenant:', firstActiveTenant.id)
446
443
  setTenant(firstActiveTenant.id)
447
- console.log('[Auth] Tenant set. Current tenant ID in API:', api.getTenantId())
448
444
  }
449
445
  }
450
446