@bagelink/auth 1.21.1 → 1.21.5
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/callbackGate.d.ts +23 -0
- package/dist/index.cjs +71 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +71 -0
- package/dist/useAuth.d.ts +2 -0
- package/package.json +1 -1
- package/src/callbackGate.test.ts +55 -0
- package/src/callbackGate.ts +68 -0
- package/src/index.ts +1 -0
- package/src/redirect.ts +7 -0
- package/src/useAuth.ts +15 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps post-login navigation and logged-out 401s quiet during SSO callback.
|
|
3
|
+
* Callback, login-token, and MFA screens own their own redirect (often
|
|
4
|
+
* `location.replace`). A LOGIN → `router.push` race mounts the app shell early.
|
|
5
|
+
*/
|
|
6
|
+
/** Route names that perform their own post-login navigation. */
|
|
7
|
+
export declare const POST_LOGIN_OWNER_ROUTES: Set<string>;
|
|
8
|
+
export declare function setSsoCallbackInProgress(active: boolean): void;
|
|
9
|
+
export declare function isSsoCallbackInProgress(): boolean;
|
|
10
|
+
export declare function authPathIsPublic(path: string): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* A named route is public only when the host listed it in `noAuthRoutes`.
|
|
13
|
+
* Before the route name exists, fall back to the current auth path.
|
|
14
|
+
*/
|
|
15
|
+
export declare function routeIsAuthPublic(route: {
|
|
16
|
+
name?: unknown;
|
|
17
|
+
path?: string;
|
|
18
|
+
} | null | undefined, noAuthRouteNames: readonly string[]): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Whether the default / host LOGIN redirect should run.
|
|
21
|
+
* Returns false during SSO callback processing and on routes that own redirect.
|
|
22
|
+
*/
|
|
23
|
+
export declare function shouldAutoRedirectAfterLogin(routeName?: string | symbol | null): boolean;
|
package/dist/index.cjs
CHANGED
|
@@ -459,6 +459,56 @@ class AuthApi {
|
|
|
459
459
|
return this.api.post(`invitations/${token}/accept`, data ?? {});
|
|
460
460
|
}
|
|
461
461
|
}
|
|
462
|
+
const POST_LOGIN_OWNER_ROUTES = /* @__PURE__ */ new Set([
|
|
463
|
+
"auth-callback",
|
|
464
|
+
"AuthCallback",
|
|
465
|
+
"Callback",
|
|
466
|
+
// legacy route name in some apps
|
|
467
|
+
"auth-login-token",
|
|
468
|
+
"auth-mfa"
|
|
469
|
+
]);
|
|
470
|
+
let ssoCallbackInProgress = false;
|
|
471
|
+
function setSsoCallbackInProgress(active) {
|
|
472
|
+
ssoCallbackInProgress = active;
|
|
473
|
+
}
|
|
474
|
+
function isSsoCallbackInProgress() {
|
|
475
|
+
return ssoCallbackInProgress;
|
|
476
|
+
}
|
|
477
|
+
const PUBLIC_AUTH_PATHS = [
|
|
478
|
+
"/login",
|
|
479
|
+
"/signup",
|
|
480
|
+
"/forgot-password",
|
|
481
|
+
"/reset-password",
|
|
482
|
+
"/resetPassword",
|
|
483
|
+
"/otp",
|
|
484
|
+
"/invitation",
|
|
485
|
+
"/unauthorized",
|
|
486
|
+
"/auth/callback",
|
|
487
|
+
"/auth/login-token"
|
|
488
|
+
];
|
|
489
|
+
function authPathIsPublic(path) {
|
|
490
|
+
const normalized = path.split("?")[0].replace(/\/+$/, "") || "/";
|
|
491
|
+
return PUBLIC_AUTH_PATHS.some((prefix) => normalized === prefix || normalized.startsWith(`${prefix}/`));
|
|
492
|
+
}
|
|
493
|
+
function routeIsAuthPublic(route, noAuthRouteNames) {
|
|
494
|
+
if (typeof (route == null ? void 0 : route.name) === "string") return noAuthRouteNames.includes(route.name);
|
|
495
|
+
const path = (route == null ? void 0 : route.path) ?? (typeof window !== "undefined" ? window.location.pathname : "");
|
|
496
|
+
return authPathIsPublic(path);
|
|
497
|
+
}
|
|
498
|
+
function shouldAutoRedirectAfterLogin(routeName) {
|
|
499
|
+
if (ssoCallbackInProgress) return false;
|
|
500
|
+
if (routeName != null && POST_LOGIN_OWNER_ROUTES.has(String(routeName))) return false;
|
|
501
|
+
return true;
|
|
502
|
+
}
|
|
503
|
+
const callbackGate = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
504
|
+
__proto__: null,
|
|
505
|
+
POST_LOGIN_OWNER_ROUTES,
|
|
506
|
+
authPathIsPublic,
|
|
507
|
+
isSsoCallbackInProgress,
|
|
508
|
+
routeIsAuthPublic,
|
|
509
|
+
setSsoCallbackInProgress,
|
|
510
|
+
shouldAutoRedirectAfterLogin
|
|
511
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
462
512
|
const INTAKE_WORKFLOW_ID = "fdba1933-0964-4850-b52a-7a4324175790";
|
|
463
513
|
const DEFAULT_AGENT_ID = "fdba1933-0964-4850-b52a-7a4324175790";
|
|
464
514
|
const providers = {
|
|
@@ -527,6 +577,9 @@ function isValidRedirect(redirectUrl, allowedPaths) {
|
|
|
527
577
|
return true;
|
|
528
578
|
}
|
|
529
579
|
async function performRedirect(router2, config) {
|
|
580
|
+
if (!shouldAutoRedirectAfterLogin(router2.currentRoute.value.name)) {
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
530
583
|
const redirect2 = getRedirectUrl(router2, config);
|
|
531
584
|
console.log("[PerformRedirect] Current route query:", router2.currentRoute.value.query);
|
|
532
585
|
console.log("[PerformRedirect] Redirect URL:", redirect2);
|
|
@@ -1100,6 +1153,11 @@ function getRedirectConfig() {
|
|
|
1100
1153
|
}
|
|
1101
1154
|
return redirectConfig;
|
|
1102
1155
|
}
|
|
1156
|
+
function isAuthPublicPath() {
|
|
1157
|
+
var _a;
|
|
1158
|
+
const route = (_a = autoRedirectRouter == null ? void 0 : autoRedirectRouter.currentRoute) == null ? void 0 : _a.value;
|
|
1159
|
+
return routeIsAuthPublic(route, (redirectConfig == null ? void 0 : redirectConfig.noAuthRoutes) ?? []);
|
|
1160
|
+
}
|
|
1103
1161
|
function createAuth(params) {
|
|
1104
1162
|
if (authApi === null) {
|
|
1105
1163
|
authApi = new AuthApi(params.baseURL, (error) => {
|
|
@@ -1209,11 +1267,16 @@ function createAuth(params) {
|
|
|
1209
1267
|
function setupAutoRedirect() {
|
|
1210
1268
|
if (!eventEmitter || !redirectConfig) return;
|
|
1211
1269
|
eventEmitter.on(AuthState.LOGIN, async () => {
|
|
1270
|
+
if (isSsoCallbackInProgress()) return;
|
|
1212
1271
|
if (!autoRedirectRouter) {
|
|
1213
1272
|
console.warn("[Auth] Auto-redirect enabled but router not set. Call auth.use(router) in your app setup.");
|
|
1214
1273
|
return;
|
|
1215
1274
|
}
|
|
1216
1275
|
const { performRedirect: performRedirect2 } = await Promise.resolve().then(() => redirect);
|
|
1276
|
+
const { shouldAutoRedirectAfterLogin: shouldAutoRedirectAfterLogin2 } = await Promise.resolve().then(() => callbackGate);
|
|
1277
|
+
if (!shouldAutoRedirectAfterLogin2(autoRedirectRouter.currentRoute.value.name)) {
|
|
1278
|
+
return;
|
|
1279
|
+
}
|
|
1217
1280
|
try {
|
|
1218
1281
|
await performRedirect2(autoRedirectRouter, redirectConfig);
|
|
1219
1282
|
} catch (error) {
|
|
@@ -1693,6 +1756,7 @@ const useAuth$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.definePro
|
|
|
1693
1756
|
createAuth,
|
|
1694
1757
|
ensureInitialAuth,
|
|
1695
1758
|
getRedirectConfig,
|
|
1759
|
+
isAuthPublicPath,
|
|
1696
1760
|
resetInitialAuth,
|
|
1697
1761
|
useAuth
|
|
1698
1762
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
@@ -1778,6 +1842,7 @@ exports.AuthState = AuthState;
|
|
|
1778
1842
|
exports.DEFAULT_AGENT_ID = DEFAULT_AGENT_ID;
|
|
1779
1843
|
exports.DEFAULT_REDIRECT_CONFIG = DEFAULT_REDIRECT_CONFIG;
|
|
1780
1844
|
exports.INTAKE_WORKFLOW_ID = INTAKE_WORKFLOW_ID;
|
|
1845
|
+
exports.POST_LOGIN_OWNER_ROUTES = POST_LOGIN_OWNER_ROUTES;
|
|
1781
1846
|
exports.PopupBlockedError = PopupBlockedError;
|
|
1782
1847
|
exports.PopupClosedError = PopupClosedError;
|
|
1783
1848
|
exports.PopupTimeoutError = PopupTimeoutError;
|
|
@@ -1785,6 +1850,7 @@ exports.SSOError = SSOError;
|
|
|
1785
1850
|
exports.StateMismatchError = StateMismatchError;
|
|
1786
1851
|
exports.accountToUser = accountToUser;
|
|
1787
1852
|
exports.authGuard = authGuard;
|
|
1853
|
+
exports.authPathIsPublic = authPathIsPublic;
|
|
1788
1854
|
exports.buildLoginQuery = buildLoginQuery;
|
|
1789
1855
|
exports.composeGuards = composeGuards;
|
|
1790
1856
|
exports.createAuth = createAuth;
|
|
@@ -1795,7 +1861,9 @@ exports.getRedirectConfig = getRedirectConfig;
|
|
|
1795
1861
|
exports.getRedirectUrl = getRedirectUrl;
|
|
1796
1862
|
exports.getSSOProvider = getSSOProvider;
|
|
1797
1863
|
exports.handleSsoPopup = handleSsoPopup;
|
|
1864
|
+
exports.isAuthPublicPath = isAuthPublicPath;
|
|
1798
1865
|
exports.isMfaRequiredError = isMfaRequiredError;
|
|
1866
|
+
exports.isSsoCallbackInProgress = isSsoCallbackInProgress;
|
|
1799
1867
|
exports.isSupportedProvider = isSupportedProvider;
|
|
1800
1868
|
exports.isValidRedirect = isValidRedirect;
|
|
1801
1869
|
exports.mfaChallengeStep = mfaChallengeStep;
|
|
@@ -1806,7 +1874,10 @@ exports.performRedirect = performRedirect;
|
|
|
1806
1874
|
exports.providers = providers;
|
|
1807
1875
|
exports.resetAuthState = resetAuthState;
|
|
1808
1876
|
exports.resetInitialAuth = resetInitialAuth;
|
|
1877
|
+
exports.routeIsAuthPublic = routeIsAuthPublic;
|
|
1809
1878
|
exports.setAuthContext = setAuthContext;
|
|
1879
|
+
exports.setSsoCallbackInProgress = setSsoCallbackInProgress;
|
|
1880
|
+
exports.shouldAutoRedirectAfterLogin = shouldAutoRedirectAfterLogin;
|
|
1810
1881
|
exports.sso = sso;
|
|
1811
1882
|
exports.ssoProvidersList = ssoProvidersList;
|
|
1812
1883
|
exports.useAuth = useAuth;
|
package/dist/index.d.ts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -457,6 +457,56 @@ class AuthApi {
|
|
|
457
457
|
return this.api.post(`invitations/${token}/accept`, data ?? {});
|
|
458
458
|
}
|
|
459
459
|
}
|
|
460
|
+
const POST_LOGIN_OWNER_ROUTES = /* @__PURE__ */ new Set([
|
|
461
|
+
"auth-callback",
|
|
462
|
+
"AuthCallback",
|
|
463
|
+
"Callback",
|
|
464
|
+
// legacy route name in some apps
|
|
465
|
+
"auth-login-token",
|
|
466
|
+
"auth-mfa"
|
|
467
|
+
]);
|
|
468
|
+
let ssoCallbackInProgress = false;
|
|
469
|
+
function setSsoCallbackInProgress(active) {
|
|
470
|
+
ssoCallbackInProgress = active;
|
|
471
|
+
}
|
|
472
|
+
function isSsoCallbackInProgress() {
|
|
473
|
+
return ssoCallbackInProgress;
|
|
474
|
+
}
|
|
475
|
+
const PUBLIC_AUTH_PATHS = [
|
|
476
|
+
"/login",
|
|
477
|
+
"/signup",
|
|
478
|
+
"/forgot-password",
|
|
479
|
+
"/reset-password",
|
|
480
|
+
"/resetPassword",
|
|
481
|
+
"/otp",
|
|
482
|
+
"/invitation",
|
|
483
|
+
"/unauthorized",
|
|
484
|
+
"/auth/callback",
|
|
485
|
+
"/auth/login-token"
|
|
486
|
+
];
|
|
487
|
+
function authPathIsPublic(path) {
|
|
488
|
+
const normalized = path.split("?")[0].replace(/\/+$/, "") || "/";
|
|
489
|
+
return PUBLIC_AUTH_PATHS.some((prefix) => normalized === prefix || normalized.startsWith(`${prefix}/`));
|
|
490
|
+
}
|
|
491
|
+
function routeIsAuthPublic(route, noAuthRouteNames) {
|
|
492
|
+
if (typeof (route == null ? void 0 : route.name) === "string") return noAuthRouteNames.includes(route.name);
|
|
493
|
+
const path = (route == null ? void 0 : route.path) ?? (typeof window !== "undefined" ? window.location.pathname : "");
|
|
494
|
+
return authPathIsPublic(path);
|
|
495
|
+
}
|
|
496
|
+
function shouldAutoRedirectAfterLogin(routeName) {
|
|
497
|
+
if (ssoCallbackInProgress) return false;
|
|
498
|
+
if (routeName != null && POST_LOGIN_OWNER_ROUTES.has(String(routeName))) return false;
|
|
499
|
+
return true;
|
|
500
|
+
}
|
|
501
|
+
const callbackGate = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
502
|
+
__proto__: null,
|
|
503
|
+
POST_LOGIN_OWNER_ROUTES,
|
|
504
|
+
authPathIsPublic,
|
|
505
|
+
isSsoCallbackInProgress,
|
|
506
|
+
routeIsAuthPublic,
|
|
507
|
+
setSsoCallbackInProgress,
|
|
508
|
+
shouldAutoRedirectAfterLogin
|
|
509
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
460
510
|
const INTAKE_WORKFLOW_ID = "fdba1933-0964-4850-b52a-7a4324175790";
|
|
461
511
|
const DEFAULT_AGENT_ID = "fdba1933-0964-4850-b52a-7a4324175790";
|
|
462
512
|
const providers = {
|
|
@@ -525,6 +575,9 @@ function isValidRedirect(redirectUrl, allowedPaths) {
|
|
|
525
575
|
return true;
|
|
526
576
|
}
|
|
527
577
|
async function performRedirect(router2, config) {
|
|
578
|
+
if (!shouldAutoRedirectAfterLogin(router2.currentRoute.value.name)) {
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
528
581
|
const redirect2 = getRedirectUrl(router2, config);
|
|
529
582
|
console.log("[PerformRedirect] Current route query:", router2.currentRoute.value.query);
|
|
530
583
|
console.log("[PerformRedirect] Redirect URL:", redirect2);
|
|
@@ -1098,6 +1151,11 @@ function getRedirectConfig() {
|
|
|
1098
1151
|
}
|
|
1099
1152
|
return redirectConfig;
|
|
1100
1153
|
}
|
|
1154
|
+
function isAuthPublicPath() {
|
|
1155
|
+
var _a;
|
|
1156
|
+
const route = (_a = autoRedirectRouter == null ? void 0 : autoRedirectRouter.currentRoute) == null ? void 0 : _a.value;
|
|
1157
|
+
return routeIsAuthPublic(route, (redirectConfig == null ? void 0 : redirectConfig.noAuthRoutes) ?? []);
|
|
1158
|
+
}
|
|
1101
1159
|
function createAuth(params) {
|
|
1102
1160
|
if (authApi === null) {
|
|
1103
1161
|
authApi = new AuthApi(params.baseURL, (error) => {
|
|
@@ -1207,11 +1265,16 @@ function createAuth(params) {
|
|
|
1207
1265
|
function setupAutoRedirect() {
|
|
1208
1266
|
if (!eventEmitter || !redirectConfig) return;
|
|
1209
1267
|
eventEmitter.on(AuthState.LOGIN, async () => {
|
|
1268
|
+
if (isSsoCallbackInProgress()) return;
|
|
1210
1269
|
if (!autoRedirectRouter) {
|
|
1211
1270
|
console.warn("[Auth] Auto-redirect enabled but router not set. Call auth.use(router) in your app setup.");
|
|
1212
1271
|
return;
|
|
1213
1272
|
}
|
|
1214
1273
|
const { performRedirect: performRedirect2 } = await Promise.resolve().then(() => redirect);
|
|
1274
|
+
const { shouldAutoRedirectAfterLogin: shouldAutoRedirectAfterLogin2 } = await Promise.resolve().then(() => callbackGate);
|
|
1275
|
+
if (!shouldAutoRedirectAfterLogin2(autoRedirectRouter.currentRoute.value.name)) {
|
|
1276
|
+
return;
|
|
1277
|
+
}
|
|
1215
1278
|
try {
|
|
1216
1279
|
await performRedirect2(autoRedirectRouter, redirectConfig);
|
|
1217
1280
|
} catch (error) {
|
|
@@ -1691,6 +1754,7 @@ const useAuth$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.definePro
|
|
|
1691
1754
|
createAuth,
|
|
1692
1755
|
ensureInitialAuth,
|
|
1693
1756
|
getRedirectConfig,
|
|
1757
|
+
isAuthPublicPath,
|
|
1694
1758
|
resetInitialAuth,
|
|
1695
1759
|
useAuth
|
|
1696
1760
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
@@ -1777,6 +1841,7 @@ export {
|
|
|
1777
1841
|
DEFAULT_AGENT_ID,
|
|
1778
1842
|
DEFAULT_REDIRECT_CONFIG,
|
|
1779
1843
|
INTAKE_WORKFLOW_ID,
|
|
1844
|
+
POST_LOGIN_OWNER_ROUTES,
|
|
1780
1845
|
PopupBlockedError,
|
|
1781
1846
|
PopupClosedError,
|
|
1782
1847
|
PopupTimeoutError,
|
|
@@ -1784,6 +1849,7 @@ export {
|
|
|
1784
1849
|
StateMismatchError,
|
|
1785
1850
|
accountToUser,
|
|
1786
1851
|
authGuard,
|
|
1852
|
+
authPathIsPublic,
|
|
1787
1853
|
buildLoginQuery,
|
|
1788
1854
|
composeGuards,
|
|
1789
1855
|
createAuth,
|
|
@@ -1794,7 +1860,9 @@ export {
|
|
|
1794
1860
|
getRedirectUrl,
|
|
1795
1861
|
getSSOProvider,
|
|
1796
1862
|
handleSsoPopup,
|
|
1863
|
+
isAuthPublicPath,
|
|
1797
1864
|
isMfaRequiredError,
|
|
1865
|
+
isSsoCallbackInProgress,
|
|
1798
1866
|
isSupportedProvider,
|
|
1799
1867
|
isValidRedirect,
|
|
1800
1868
|
mfaChallengeStep,
|
|
@@ -1805,7 +1873,10 @@ export {
|
|
|
1805
1873
|
providers,
|
|
1806
1874
|
resetAuthState,
|
|
1807
1875
|
resetInitialAuth,
|
|
1876
|
+
routeIsAuthPublic,
|
|
1808
1877
|
setAuthContext,
|
|
1878
|
+
setSsoCallbackInProgress,
|
|
1879
|
+
shouldAutoRedirectAfterLogin,
|
|
1809
1880
|
sso,
|
|
1810
1881
|
ssoProvidersList,
|
|
1811
1882
|
useAuth
|
package/dist/useAuth.d.ts
CHANGED
|
@@ -53,6 +53,8 @@ export declare function resetInitialAuth(): void;
|
|
|
53
53
|
* Used internally by router guard
|
|
54
54
|
*/
|
|
55
55
|
export declare function getRedirectConfig(): NormalizedRedirectConfig;
|
|
56
|
+
/** True on login/signup/callback screens, where a logged-out 401 is expected. */
|
|
57
|
+
export declare function isAuthPublicPath(): boolean;
|
|
56
58
|
export declare function createAuth(params: InitParams): AuthInstance;
|
|
57
59
|
export declare function useAuth(): {
|
|
58
60
|
user: import('vue').ComputedRef<User | null>;
|
package/package.json
CHANGED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from 'vitest'
|
|
2
|
+
import {
|
|
3
|
+
authPathIsPublic,
|
|
4
|
+
isSsoCallbackInProgress,
|
|
5
|
+
routeIsAuthPublic,
|
|
6
|
+
setSsoCallbackInProgress,
|
|
7
|
+
shouldAutoRedirectAfterLogin,
|
|
8
|
+
} from './callbackGate'
|
|
9
|
+
|
|
10
|
+
describe('SSO callback gate', () => {
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
setSsoCallbackInProgress(false)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('tracks whether an SSO callback is in progress', () => {
|
|
16
|
+
expect(isSsoCallbackInProgress()).toBe(false)
|
|
17
|
+
setSsoCallbackInProgress(true)
|
|
18
|
+
expect(isSsoCallbackInProgress()).toBe(true)
|
|
19
|
+
expect(shouldAutoRedirectAfterLogin('home')).toBe(false)
|
|
20
|
+
setSsoCallbackInProgress(false)
|
|
21
|
+
expect(shouldAutoRedirectAfterLogin('home')).toBe(true)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('skips soft redirect on post-login owner routes', () => {
|
|
25
|
+
expect(shouldAutoRedirectAfterLogin('auth-callback')).toBe(false)
|
|
26
|
+
expect(shouldAutoRedirectAfterLogin('AuthCallback')).toBe(false)
|
|
27
|
+
expect(shouldAutoRedirectAfterLogin('Callback')).toBe(false)
|
|
28
|
+
expect(shouldAutoRedirectAfterLogin('auth-mfa')).toBe(false)
|
|
29
|
+
expect(shouldAutoRedirectAfterLogin('auth-login-token')).toBe(false)
|
|
30
|
+
expect(shouldAutoRedirectAfterLogin('auth-login')).toBe(true)
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
describe('auth public paths', () => {
|
|
35
|
+
const names = ['auth-login', 'auth-callback']
|
|
36
|
+
|
|
37
|
+
it('treats configured route names as public', () => {
|
|
38
|
+
expect(routeIsAuthPublic({ name: 'auth-login', path: '/login' }, names)).toBe(true)
|
|
39
|
+
expect(routeIsAuthPublic({ name: 'sessions', path: '/sessions' }, names)).toBe(false)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('uses the path only before a route name exists', () => {
|
|
43
|
+
expect(routeIsAuthPublic({ path: '/auth/callback' }, names)).toBe(true)
|
|
44
|
+
expect(routeIsAuthPublic({ path: '/login-history' }, names)).toBe(false)
|
|
45
|
+
expect(authPathIsPublic('/login')).toBe(true)
|
|
46
|
+
expect(authPathIsPublic('/invitation/abc')).toBe(true)
|
|
47
|
+
expect(authPathIsPublic('/resetPassword')).toBe(true)
|
|
48
|
+
expect(authPathIsPublic('/')).toBe(false)
|
|
49
|
+
expect(authPathIsPublic('/people')).toBe(false)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('does not treat a named protected route as public because of its path', () => {
|
|
53
|
+
expect(routeIsAuthPublic({ name: 'sessions', path: '/login' }, names)).toBe(false)
|
|
54
|
+
})
|
|
55
|
+
})
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps post-login navigation and logged-out 401s quiet during SSO callback.
|
|
3
|
+
* Callback, login-token, and MFA screens own their own redirect (often
|
|
4
|
+
* `location.replace`). A LOGIN → `router.push` race mounts the app shell early.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Route names that perform their own post-login navigation. */
|
|
8
|
+
export const POST_LOGIN_OWNER_ROUTES = new Set([
|
|
9
|
+
'auth-callback',
|
|
10
|
+
'AuthCallback',
|
|
11
|
+
'Callback', // legacy route name in some apps
|
|
12
|
+
'auth-login-token',
|
|
13
|
+
'auth-mfa',
|
|
14
|
+
])
|
|
15
|
+
|
|
16
|
+
let ssoCallbackInProgress = false
|
|
17
|
+
|
|
18
|
+
export function setSsoCallbackInProgress(active: boolean): void {
|
|
19
|
+
ssoCallbackInProgress = active
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function isSsoCallbackInProgress(): boolean {
|
|
23
|
+
return ssoCallbackInProgress
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Paths used before the router has resolved a named route. */
|
|
27
|
+
const PUBLIC_AUTH_PATHS = [
|
|
28
|
+
'/login',
|
|
29
|
+
'/signup',
|
|
30
|
+
'/forgot-password',
|
|
31
|
+
'/reset-password',
|
|
32
|
+
'/resetPassword',
|
|
33
|
+
'/otp',
|
|
34
|
+
'/invitation',
|
|
35
|
+
'/unauthorized',
|
|
36
|
+
'/auth/callback',
|
|
37
|
+
'/auth/login-token',
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
export function authPathIsPublic(path: string): boolean {
|
|
41
|
+
const normalized = path.split('?')[0].replace(/\/+$/, '') || '/'
|
|
42
|
+
return PUBLIC_AUTH_PATHS.some(prefix => normalized === prefix || normalized.startsWith(`${prefix}/`))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A named route is public only when the host listed it in `noAuthRoutes`.
|
|
47
|
+
* Before the route name exists, fall back to the current auth path.
|
|
48
|
+
*/
|
|
49
|
+
export function routeIsAuthPublic(
|
|
50
|
+
route: { name?: unknown, path?: string } | null | undefined,
|
|
51
|
+
noAuthRouteNames: readonly string[],
|
|
52
|
+
): boolean {
|
|
53
|
+
if (typeof route?.name === 'string') return noAuthRouteNames.includes(route.name)
|
|
54
|
+
const path = route?.path ?? (typeof window !== 'undefined' ? window.location.pathname : '')
|
|
55
|
+
return authPathIsPublic(path)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Whether the default / host LOGIN redirect should run.
|
|
60
|
+
* Returns false during SSO callback processing and on routes that own redirect.
|
|
61
|
+
*/
|
|
62
|
+
export function shouldAutoRedirectAfterLogin(
|
|
63
|
+
routeName?: string | symbol | null,
|
|
64
|
+
): boolean {
|
|
65
|
+
if (ssoCallbackInProgress) return false
|
|
66
|
+
if (routeName != null && POST_LOGIN_OWNER_ROUTES.has(String(routeName))) return false
|
|
67
|
+
return true
|
|
68
|
+
}
|
package/src/index.ts
CHANGED
package/src/redirect.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Router } from 'vue-router'
|
|
2
|
+
import { shouldAutoRedirectAfterLogin } from './callbackGate'
|
|
2
3
|
import type { NormalizedRedirectConfig } from './types/redirect'
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -63,6 +64,12 @@ export async function performRedirect(
|
|
|
63
64
|
router: Router,
|
|
64
65
|
config: NormalizedRedirectConfig,
|
|
65
66
|
): Promise<void> {
|
|
67
|
+
// SSO / login-token / MFA screens own post-login navigation (often a hard
|
|
68
|
+
// redirect). Soft-pushing here races them and flashes auth errors.
|
|
69
|
+
if (!shouldAutoRedirectAfterLogin(router.currentRoute.value.name)) {
|
|
70
|
+
return
|
|
71
|
+
}
|
|
72
|
+
|
|
66
73
|
const redirect = getRedirectUrl(router, config)
|
|
67
74
|
console.log('[PerformRedirect] Current route query:', router.currentRoute.value.query)
|
|
68
75
|
console.log('[PerformRedirect] Redirect URL:', redirect)
|
package/src/useAuth.ts
CHANGED
|
@@ -30,6 +30,7 @@ import type {
|
|
|
30
30
|
import type { RedirectConfig, NormalizedRedirectConfig } from './types/redirect'
|
|
31
31
|
import { ref, computed } from 'vue'
|
|
32
32
|
import { AuthApi } from './api'
|
|
33
|
+
import { isSsoCallbackInProgress, routeIsAuthPublic } from './callbackGate'
|
|
33
34
|
import { setAuthContext, sso } from './sso'
|
|
34
35
|
import { AuthState, accountToUser } from './types'
|
|
35
36
|
import { normalizeRedirectConfig } from './types/redirect'
|
|
@@ -120,6 +121,12 @@ export function getRedirectConfig(): NormalizedRedirectConfig {
|
|
|
120
121
|
return redirectConfig
|
|
121
122
|
}
|
|
122
123
|
|
|
124
|
+
/** True on login/signup/callback screens, where a logged-out 401 is expected. */
|
|
125
|
+
export function isAuthPublicPath(): boolean {
|
|
126
|
+
const route = autoRedirectRouter?.currentRoute?.value as { name?: unknown, path?: string } | undefined
|
|
127
|
+
return routeIsAuthPublic(route, redirectConfig?.noAuthRoutes ?? [])
|
|
128
|
+
}
|
|
129
|
+
|
|
123
130
|
// Initialize auth
|
|
124
131
|
export function createAuth(params: InitParams): AuthInstance {
|
|
125
132
|
if (authApi === null) {
|
|
@@ -253,6 +260,9 @@ function setupAutoRedirect() {
|
|
|
253
260
|
if (!eventEmitter || !redirectConfig) return
|
|
254
261
|
|
|
255
262
|
eventEmitter.on(AuthState.LOGIN, async () => {
|
|
263
|
+
// SSO callback pages hard-redirect themselves. A router push here races that navigation.
|
|
264
|
+
if (isSsoCallbackInProgress()) return
|
|
265
|
+
|
|
256
266
|
// Only auto-redirect if router is available
|
|
257
267
|
if (!autoRedirectRouter) {
|
|
258
268
|
console.warn('[Auth] Auto-redirect enabled but router not set. Call auth.use(router) in your app setup.')
|
|
@@ -261,6 +271,11 @@ function setupAutoRedirect() {
|
|
|
261
271
|
|
|
262
272
|
// Dynamic import to avoid circular dependency
|
|
263
273
|
const { performRedirect } = await import('./redirect')
|
|
274
|
+
const { shouldAutoRedirectAfterLogin } = await import('./callbackGate')
|
|
275
|
+
|
|
276
|
+
if (!shouldAutoRedirectAfterLogin(autoRedirectRouter.currentRoute.value.name)) {
|
|
277
|
+
return
|
|
278
|
+
}
|
|
264
279
|
|
|
265
280
|
try {
|
|
266
281
|
await performRedirect(autoRedirectRouter, redirectConfig!)
|