@bagelink/auth 1.7.72 → 1.7.76

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
@@ -1079,7 +1079,7 @@ const _sfc_main$4 = /* @__PURE__ */ vue.defineComponent({
1079
1079
  const authResponse = vue.ref(null);
1080
1080
  const { sso: sso2, user, accountInfo: accountInfo2 } = useAuth();
1081
1081
  const route = vueRouter.useRoute();
1082
- const router = vueRouter.useRouter();
1082
+ const router2 = vueRouter.useRouter();
1083
1083
  const providerInfo = vue.computed(() => {
1084
1084
  if (provider.value === null) return null;
1085
1085
  return providers[provider.value];
@@ -1089,7 +1089,7 @@ const _sfc_main$4 = /* @__PURE__ */ vue.defineComponent({
1089
1089
  try {
1090
1090
  await sso2.handleLinkCallback();
1091
1091
  success.value = true;
1092
- setTimeout(() => router.push("/"), timeout);
1092
+ setTimeout(() => router2.push("/"), timeout);
1093
1093
  } catch (err) {
1094
1094
  const errorMessage = err instanceof Error ? err.message : "Failed to link account";
1095
1095
  error.value = errorMessage;
@@ -1108,7 +1108,7 @@ const _sfc_main$4 = /* @__PURE__ */ vue.defineComponent({
1108
1108
  } else {
1109
1109
  authResponse.value = response;
1110
1110
  success.value = true;
1111
- setTimeout(() => router.push("/"), timeout);
1111
+ setTimeout(() => router2.push("/"), timeout);
1112
1112
  }
1113
1113
  } catch (err) {
1114
1114
  const errorMessage = err instanceof Error ? err.message : "Authentication failed";
@@ -1239,10 +1239,10 @@ const _sfc_main$3 = /* @__PURE__ */ vue.defineComponent({
1239
1239
  cardShadow: { type: Boolean, default: true }
1240
1240
  },
1241
1241
  setup(__props) {
1242
- const router = vueRouter.useRouter();
1242
+ const router2 = vueRouter.useRouter();
1243
1243
  function switchForm(form) {
1244
1244
  if (form === "login") {
1245
- router.push("/login");
1245
+ router2.push("/login");
1246
1246
  }
1247
1247
  }
1248
1248
  return (_ctx, _cache) => {
@@ -1283,12 +1283,12 @@ const _sfc_main$2 = /* @__PURE__ */ vue.defineComponent({
1283
1283
  cardShadow: { type: Boolean, default: true }
1284
1284
  },
1285
1285
  setup(__props) {
1286
- const router = vueRouter.useRouter();
1286
+ const router2 = vueRouter.useRouter();
1287
1287
  function switchForm(form) {
1288
1288
  if (form === "signup") {
1289
- router.push("/signup");
1289
+ router2.push("/signup");
1290
1290
  } else if (form === "forgot-password") {
1291
- router.push("/forgot-password");
1291
+ router2.push("/forgot-password");
1292
1292
  }
1293
1293
  }
1294
1294
  return (_ctx, _cache) => {
@@ -1329,12 +1329,12 @@ const _sfc_main$1 = /* @__PURE__ */ vue.defineComponent({
1329
1329
  cardShadow: { type: Boolean, default: true }
1330
1330
  },
1331
1331
  setup(__props) {
1332
- const router = vueRouter.useRouter();
1332
+ const router2 = vueRouter.useRouter();
1333
1333
  const route = vueRouter.useRoute();
1334
1334
  const token = vue.computed(() => route.query.token);
1335
1335
  function switchForm(form) {
1336
1336
  if (form === "login") {
1337
- router.push("/login");
1337
+ router2.push("/login");
1338
1338
  }
1339
1339
  }
1340
1340
  return (_ctx, _cache) => {
@@ -1366,10 +1366,10 @@ const _sfc_main = /* @__PURE__ */ vue.defineComponent({
1366
1366
  cardShadow: { type: Boolean, default: true }
1367
1367
  },
1368
1368
  setup(__props) {
1369
- const router = vueRouter.useRouter();
1369
+ const router2 = vueRouter.useRouter();
1370
1370
  function switchForm(form) {
1371
1371
  if (form === "login") {
1372
- router.push("/login");
1372
+ router2.push("/login");
1373
1373
  }
1374
1374
  }
1375
1375
  return (_ctx, _cache) => {
@@ -1391,70 +1391,56 @@ const _sfc_main = /* @__PURE__ */ vue.defineComponent({
1391
1391
  };
1392
1392
  }
1393
1393
  });
1394
- function createAuthRoutes(config = {}) {
1395
- const {
1396
- basePath = "",
1397
- namePrefix = "",
1398
- routeNames = {},
1399
- layout
1400
- } = config;
1401
- const createRouteName = (name) => namePrefix ? `${namePrefix}${name}` : name;
1402
- const routes = [
1403
- {
1404
- path: `${basePath}/login`,
1405
- name: routeNames.login || createRouteName("Login"),
1406
- component: () => Promise.resolve().then(() => require("./LoginPage-hv1wc54S.cjs")),
1407
- meta: { requiresAuth: false }
1408
- },
1409
- {
1410
- path: `${basePath}/signup`,
1411
- name: routeNames.signup || createRouteName("Signup"),
1412
- component: () => Promise.resolve().then(() => require("./SignupPage-m36w9PLJ.cjs")),
1413
- meta: { requiresAuth: false }
1414
- },
1415
- {
1416
- path: `${basePath}/forgot-password`,
1417
- name: routeNames.forgotPassword || createRouteName("ForgotPassword"),
1418
- component: () => Promise.resolve().then(() => require("./ForgotPasswordPage-BV9tyhHl.cjs")),
1419
- meta: { requiresAuth: false }
1420
- },
1421
- {
1422
- path: `${basePath}/reset-password`,
1423
- name: routeNames.resetPassword || createRouteName("ResetPassword"),
1424
- component: () => Promise.resolve().then(() => require("./ResetPasswordPage-COPrJmW8.cjs")),
1425
- meta: { requiresAuth: false }
1426
- },
1427
- {
1428
- path: `${basePath}/callback`,
1429
- name: routeNames.callback || createRouteName("AuthCallback"),
1430
- component: () => Promise.resolve().then(() => require("./Callback-BHqVaZZm.cjs")),
1431
- meta: { requiresAuth: false }
1432
- }
1433
- ];
1434
- if (layout) {
1435
- return [{
1436
- path: basePath,
1437
- component: layout,
1438
- children: routes.map((route) => ({
1439
- ...route,
1440
- path: route.path.replace(basePath, "")
1441
- }))
1442
- }];
1394
+ function getRedirectUrl(router2, config) {
1395
+ const redirect2 = router2.currentRoute.value.query[config.queryKey];
1396
+ return redirect2 || config.fallback;
1397
+ }
1398
+ function isValidRedirect(redirectUrl, allowedPaths) {
1399
+ if (!redirectUrl) {
1400
+ return false;
1443
1401
  }
1444
- return routes;
1402
+ if (redirectUrl.startsWith("http://") || redirectUrl.startsWith("https://")) {
1403
+ return false;
1404
+ }
1405
+ if (redirectUrl.startsWith("//")) {
1406
+ return false;
1407
+ }
1408
+ if (redirectUrl.startsWith("javascript:") || redirectUrl.startsWith("data:")) {
1409
+ return false;
1410
+ }
1411
+ if (!redirectUrl.startsWith("/")) {
1412
+ return false;
1413
+ }
1414
+ if (allowedPaths && allowedPaths.length > 0) {
1415
+ return allowedPaths.some((pattern) => pattern.test(redirectUrl));
1416
+ }
1417
+ return true;
1445
1418
  }
1446
- function createAuthGuard(config = {}) {
1447
- const { redirectTo = "/" } = config;
1448
- return async (to, _from, next) => {
1449
- const { useAuth: useAuth2 } = await Promise.resolve().then(() => useAuth$1);
1450
- const { user } = useAuth2();
1451
- if (to.meta.requiresAuth === false && user.value) {
1452
- next(redirectTo);
1453
- } else {
1454
- next();
1455
- }
1456
- };
1419
+ async function performRedirect(router2, config) {
1420
+ const redirect2 = getRedirectUrl(router2, config);
1421
+ if (redirect2 !== config.fallback && !isValidRedirect(redirect2, config.allowedPaths)) {
1422
+ console.warn("[Auth] Invalid redirect URL detected, using fallback:", redirect2);
1423
+ await router2.push(config.fallback);
1424
+ return;
1425
+ }
1426
+ await router2.push(redirect2);
1457
1427
  }
1428
+ function buildLoginQuery(currentPath, config) {
1429
+ if (!config.preserveRedirect) {
1430
+ return {};
1431
+ }
1432
+ if (isValidRedirect(currentPath, config.allowedPaths)) {
1433
+ return { [config.queryKey]: currentPath };
1434
+ }
1435
+ return {};
1436
+ }
1437
+ const redirect = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
1438
+ __proto__: null,
1439
+ buildLoginQuery,
1440
+ getRedirectUrl,
1441
+ isValidRedirect,
1442
+ performRedirect
1443
+ }, Symbol.toStringTag, { value: "Module" }));
1458
1444
  let authApiRef = null;
1459
1445
  function setAuthContext(authApi2) {
1460
1446
  authApiRef = authApi2;
@@ -1902,9 +1888,34 @@ function accountToUser(account) {
1902
1888
  lastLogin: account.last_login
1903
1889
  };
1904
1890
  }
1891
+ const DEFAULT_REDIRECT_CONFIG = {
1892
+ queryKey: "redirect",
1893
+ fallback: "/",
1894
+ noAuthRoutes: ["Login", "Signup", "ForgotPassword", "ResetPassword", "Callback"],
1895
+ authenticatedRedirect: "/",
1896
+ loginRoute: "Login",
1897
+ authMetaKey: "auth",
1898
+ autoRedirect: true,
1899
+ preserveRedirect: true
1900
+ };
1901
+ function normalizeRedirectConfig(config) {
1902
+ return {
1903
+ ...DEFAULT_REDIRECT_CONFIG,
1904
+ ...config
1905
+ };
1906
+ }
1905
1907
  let authApi = null;
1906
1908
  let eventEmitter = null;
1909
+ let redirectConfig = null;
1910
+ let autoRedirectRouter = null;
1911
+ let cachedAuthGuard = null;
1907
1912
  const accountInfo = vue.ref(null);
1913
+ function getRedirectConfig() {
1914
+ if (!redirectConfig) {
1915
+ throw new Error("Redirect config not initialized. Did you call createAuth with redirect config?");
1916
+ }
1917
+ return redirectConfig;
1918
+ }
1908
1919
  function createAuth(params) {
1909
1920
  if (authApi === null) {
1910
1921
  authApi = new AuthApi(params.baseURL);
@@ -1912,7 +1923,13 @@ function createAuth(params) {
1912
1923
  if (eventEmitter === null) {
1913
1924
  eventEmitter = new EventEmitter();
1914
1925
  }
1915
- return {
1926
+ if (params.redirect) {
1927
+ redirectConfig = normalizeRedirectConfig(params.redirect);
1928
+ }
1929
+ if (redirectConfig == null ? void 0 : redirectConfig.autoRedirect) {
1930
+ setupAutoRedirect();
1931
+ }
1932
+ const authInstance = {
1916
1933
  // Event listener methods
1917
1934
  on(event, handler) {
1918
1935
  if (eventEmitter) {
@@ -1929,10 +1946,89 @@ function createAuth(params) {
1929
1946
  eventEmitter.removeAllListeners(event);
1930
1947
  }
1931
1948
  },
1949
+ /**
1950
+ * Connect external dependencies like Vue Router
1951
+ * Automatically sets up router guard when router is provided
1952
+ * @param dependency - Vue Router instance or other plugins
1953
+ * @param options - Configuration options
1954
+ * @param options.guard - Whether to automatically set up auth guard (default: true)
1955
+ * @example
1956
+ * ```ts
1957
+ * // Auto setup (default)
1958
+ * auth.use(router)
1959
+ *
1960
+ * // Manual guard control (for custom composition)
1961
+ * auth.use(router, { guard: false })
1962
+ * router.beforeEach(async (to, from, next) => {
1963
+ * // Custom logic first
1964
+ * if (!hasOrgAccess(to)) return next('/no-access')
1965
+ * // Then run auth guard
1966
+ * return auth.routerGuard()(to, from, next)
1967
+ * })
1968
+ * ```
1969
+ */
1970
+ use(dependency, options = {}) {
1971
+ const { guard = true } = options;
1972
+ if (dependency && (dependency.beforeEach || dependency.push || dependency.currentRoute)) {
1973
+ autoRedirectRouter = dependency;
1974
+ if (guard) {
1975
+ dependency.beforeEach(authInstance.routerGuard());
1976
+ }
1977
+ }
1978
+ return authInstance;
1979
+ },
1980
+ /**
1981
+ * Create a Vue Router navigation guard for authentication
1982
+ * Protects routes requiring authentication and handles redirect logic
1983
+ * Note: Automatically called by auth.use(router), only use directly for custom setups
1984
+ * @example
1985
+ * ```ts
1986
+ * // Automatic (recommended)
1987
+ * auth.use(router)
1988
+ *
1989
+ * // Manual (for custom setups)
1990
+ * router.beforeEach(auth.routerGuard())
1991
+ * ```
1992
+ */
1993
+ routerGuard() {
1994
+ if (cachedAuthGuard === null) {
1995
+ cachedAuthGuard = async (to, from, next) => {
1996
+ const { authGuard: authGuard2 } = await Promise.resolve().then(() => router);
1997
+ const guard = authGuard2();
1998
+ cachedAuthGuard = guard;
1999
+ return guard(to, from, next);
2000
+ };
2001
+ }
2002
+ return cachedAuthGuard;
2003
+ },
2004
+ /**
2005
+ * Vue plugin install method
2006
+ * Makes auth available globally as $auth
2007
+ * @example
2008
+ * ```ts
2009
+ * app.use(auth)
2010
+ * ```
2011
+ */
1932
2012
  install(app) {
1933
2013
  app.config.globalProperties.$auth = useAuth();
1934
2014
  }
1935
2015
  };
2016
+ return authInstance;
2017
+ }
2018
+ function setupAutoRedirect() {
2019
+ if (!eventEmitter || !redirectConfig) return;
2020
+ eventEmitter.on(AuthState.LOGIN, async () => {
2021
+ if (!autoRedirectRouter) {
2022
+ console.warn("[Auth] Auto-redirect enabled but router not set. Call setAuthRouter(router) in your app setup.");
2023
+ return;
2024
+ }
2025
+ const { performRedirect: performRedirect2 } = await Promise.resolve().then(() => redirect);
2026
+ try {
2027
+ await performRedirect2(autoRedirectRouter, redirectConfig);
2028
+ } catch (error) {
2029
+ console.error("[Auth] Auto-redirect error:", error);
2030
+ }
2031
+ });
1936
2032
  }
1937
2033
  function useAuth() {
1938
2034
  if (authApi === null) {
@@ -2182,12 +2278,140 @@ function useAuth() {
2182
2278
  const useAuth$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
2183
2279
  __proto__: null,
2184
2280
  createAuth,
2281
+ getRedirectConfig,
2185
2282
  useAuth
2186
2283
  }, Symbol.toStringTag, { value: "Module" }));
2284
+ let authInitialized = false;
2285
+ function resetAuthState() {
2286
+ authInitialized = false;
2287
+ }
2288
+ function authGuard() {
2289
+ return async function guard(to, _from, next) {
2290
+ const auth = useAuth();
2291
+ const config = getRedirectConfig();
2292
+ const requiresAuth = to.meta[config.authMetaKey];
2293
+ const requiresNoAuth = config.noAuthRoutes.includes(to.name);
2294
+ try {
2295
+ if (!authInitialized) {
2296
+ await auth.checkAuth();
2297
+ authInitialized = true;
2298
+ }
2299
+ const isAuthenticated = !!auth.user.value;
2300
+ if (isAuthenticated && requiresNoAuth) {
2301
+ next(config.authenticatedRedirect);
2302
+ return;
2303
+ }
2304
+ if (!isAuthenticated && requiresAuth) {
2305
+ const query = buildLoginQuery(to.fullPath, config);
2306
+ next({
2307
+ name: config.loginRoute,
2308
+ query
2309
+ });
2310
+ return;
2311
+ }
2312
+ next();
2313
+ } catch (error) {
2314
+ console.error("[Auth Guard] Error:", error);
2315
+ next();
2316
+ }
2317
+ };
2318
+ }
2319
+ function composeGuards(guards) {
2320
+ return async (to, from, next) => {
2321
+ let guardIndex = 0;
2322
+ const runNextGuard = async () => {
2323
+ if (guardIndex >= guards.length) {
2324
+ next();
2325
+ return;
2326
+ }
2327
+ const guard = guards[guardIndex];
2328
+ guardIndex++;
2329
+ await guard(to, from, (result) => {
2330
+ if (result !== void 0) {
2331
+ next(result);
2332
+ } else {
2333
+ runNextGuard();
2334
+ }
2335
+ });
2336
+ };
2337
+ await runNextGuard();
2338
+ };
2339
+ }
2340
+ const router = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
2341
+ __proto__: null,
2342
+ authGuard,
2343
+ composeGuards,
2344
+ resetAuthState
2345
+ }, Symbol.toStringTag, { value: "Module" }));
2346
+ function createAuthRoutes(config = {}) {
2347
+ const {
2348
+ basePath = "",
2349
+ namePrefix = "",
2350
+ routeNames = {},
2351
+ layout
2352
+ } = config;
2353
+ const createRouteName = (name) => namePrefix ? `${namePrefix}${name}` : name;
2354
+ const routes = [
2355
+ {
2356
+ path: `${basePath}/login`,
2357
+ name: routeNames.login || createRouteName("Login"),
2358
+ component: () => Promise.resolve().then(() => require("./LoginPage-hv1wc54S.cjs")),
2359
+ meta: { requiresAuth: false }
2360
+ },
2361
+ {
2362
+ path: `${basePath}/signup`,
2363
+ name: routeNames.signup || createRouteName("Signup"),
2364
+ component: () => Promise.resolve().then(() => require("./SignupPage-m36w9PLJ.cjs")),
2365
+ meta: { requiresAuth: false }
2366
+ },
2367
+ {
2368
+ path: `${basePath}/forgot-password`,
2369
+ name: routeNames.forgotPassword || createRouteName("ForgotPassword"),
2370
+ component: () => Promise.resolve().then(() => require("./ForgotPasswordPage-BV9tyhHl.cjs")),
2371
+ meta: { requiresAuth: false }
2372
+ },
2373
+ {
2374
+ path: `${basePath}/reset-password`,
2375
+ name: routeNames.resetPassword || createRouteName("ResetPassword"),
2376
+ component: () => Promise.resolve().then(() => require("./ResetPasswordPage-COPrJmW8.cjs")),
2377
+ meta: { requiresAuth: false }
2378
+ },
2379
+ {
2380
+ path: `${basePath}/callback`,
2381
+ name: routeNames.callback || createRouteName("AuthCallback"),
2382
+ component: () => Promise.resolve().then(() => require("./Callback-BHqVaZZm.cjs")),
2383
+ meta: { requiresAuth: false }
2384
+ }
2385
+ ];
2386
+ if (layout) {
2387
+ return [{
2388
+ path: basePath,
2389
+ component: layout,
2390
+ children: routes.map((route) => ({
2391
+ ...route,
2392
+ path: route.path.replace(basePath, "")
2393
+ }))
2394
+ }];
2395
+ }
2396
+ return routes;
2397
+ }
2398
+ function createAuthGuard(config = {}) {
2399
+ const { redirectTo = "/" } = config;
2400
+ return async (to, _from, next) => {
2401
+ const { useAuth: useAuth2 } = await Promise.resolve().then(() => useAuth$1);
2402
+ const { user } = useAuth2();
2403
+ if (to.meta.requiresAuth === false && user.value) {
2404
+ next(redirectTo);
2405
+ } else {
2406
+ next();
2407
+ }
2408
+ };
2409
+ }
2187
2410
  exports.AuthApi = AuthApi;
2188
2411
  exports.AuthState = AuthState;
2189
2412
  exports.Callback = _sfc_main$4;
2190
2413
  exports.DEFAULT_AGENT_ID = DEFAULT_AGENT_ID;
2414
+ exports.DEFAULT_REDIRECT_CONFIG = DEFAULT_REDIRECT_CONFIG;
2191
2415
  exports.ForgotPasswordForm = _sfc_main$8;
2192
2416
  exports.ForgotPasswordPage = _sfc_main$3;
2193
2417
  exports.INTAKE_WORKFLOW_ID = INTAKE_WORKFLOW_ID;
@@ -2203,13 +2427,22 @@ exports.SignupForm = _sfc_main$5;
2203
2427
  exports.SignupPage = _sfc_main;
2204
2428
  exports.StateMismatchError = StateMismatchError;
2205
2429
  exports.accountToUser = accountToUser;
2430
+ exports.authGuard = authGuard;
2431
+ exports.buildLoginQuery = buildLoginQuery;
2432
+ exports.composeGuards = composeGuards;
2206
2433
  exports.createAuth = createAuth;
2207
2434
  exports.createAuthGuard = createAuthGuard;
2208
2435
  exports.createAuthRoutes = createAuthRoutes;
2209
2436
  exports.getAllSSOProviders = getAllSSOProviders;
2437
+ exports.getRedirectConfig = getRedirectConfig;
2438
+ exports.getRedirectUrl = getRedirectUrl;
2210
2439
  exports.getSSOProvider = getSSOProvider;
2211
2440
  exports.isSupportedProvider = isSupportedProvider;
2441
+ exports.isValidRedirect = isValidRedirect;
2442
+ exports.normalizeRedirectConfig = normalizeRedirectConfig;
2443
+ exports.performRedirect = performRedirect;
2212
2444
  exports.providers = providers;
2445
+ exports.resetAuthState = resetAuthState;
2213
2446
  exports.setAuthContext = setAuthContext;
2214
2447
  exports.sso = sso;
2215
2448
  exports.ssoProvidersList = ssoProvidersList;
package/dist/index.d.ts CHANGED
@@ -9,8 +9,11 @@ export { default as ForgotPasswordPage } from './pages/ForgotPasswordPage.vue';
9
9
  export { default as LoginPage } from './pages/LoginPage.vue';
10
10
  export { default as ResetPasswordPage } from './pages/ResetPasswordPage.vue';
11
11
  export { default as SignupPage } from './pages/SignupPage.vue';
12
+ export * from './redirect';
13
+ export * from './router';
12
14
  export * from './routes';
13
15
  export * from './sso';
14
16
  export * from './types';
15
17
  export type { ForgotPasswordTexts, LoginTexts, ResetPasswordTexts, SignupTexts, } from './types/';
18
+ export * from './types/redirect';
16
19
  export * from './useAuth';