@bagelink/auth 1.21.1 → 1.21.3
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 +17 -0
- package/dist/index.cjs +43 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +43 -0
- package/package.json +1 -1
- package/src/callbackGate.test.ts +36 -0
- package/src/callbackGate.ts +42 -0
- package/src/index.ts +1 -0
- package/src/redirect.ts +7 -0
- package/src/useAuth.ts +5 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gates post-login soft-navigation and expected-401 toast noise during SSO
|
|
3
|
+
* callback. The callback view owns a hard redirect (`location.replace`); any
|
|
4
|
+
* LOGIN → `router.push` race mounts the app shell early and flashes
|
|
5
|
+
* "Authentication required" from premature API calls.
|
|
6
|
+
*/
|
|
7
|
+
/** Route names that perform their own post-login navigation. */
|
|
8
|
+
export declare const POST_LOGIN_OWNER_ROUTES: Set<string>;
|
|
9
|
+
export declare function setSsoCallbackInProgress(value: boolean): void;
|
|
10
|
+
export declare function isSsoCallbackInProgress(): boolean;
|
|
11
|
+
/** Public auth paths where a 401 is expected noise, not a user-facing failure. */
|
|
12
|
+
export declare function isAuthPublicPath(pathname?: string): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Whether the default / host LOGIN redirect should run.
|
|
15
|
+
* Returns false during SSO callback processing and on routes that own redirect.
|
|
16
|
+
*/
|
|
17
|
+
export declare function shouldAutoRedirectAfterLogin(routeName?: string | symbol | null): boolean;
|
package/dist/index.cjs
CHANGED
|
@@ -501,6 +501,37 @@ function isMfaRequiredError(error) {
|
|
|
501
501
|
const code = ((_a = response == null ? void 0 : response.data) == null ? void 0 : _a.code) ?? ((_c = (_b = response == null ? void 0 : response.data) == null ? void 0 : _b.detail) == null ? void 0 : _c.code);
|
|
502
502
|
return (response == null ? void 0 : response.status) === 403 && (code === "mfa_required" || code === "mfa_enrollment_required");
|
|
503
503
|
}
|
|
504
|
+
const POST_LOGIN_OWNER_ROUTES = /* @__PURE__ */ new Set([
|
|
505
|
+
"auth-callback",
|
|
506
|
+
"AuthCallback",
|
|
507
|
+
"Callback",
|
|
508
|
+
// legacy route name in some apps
|
|
509
|
+
"auth-login-token",
|
|
510
|
+
"auth-mfa"
|
|
511
|
+
]);
|
|
512
|
+
let ssoCallbackInProgress = false;
|
|
513
|
+
function setSsoCallbackInProgress(value) {
|
|
514
|
+
ssoCallbackInProgress = value;
|
|
515
|
+
}
|
|
516
|
+
function isSsoCallbackInProgress() {
|
|
517
|
+
return ssoCallbackInProgress;
|
|
518
|
+
}
|
|
519
|
+
function isAuthPublicPath(pathname = typeof window !== "undefined" ? window.location.pathname : "") {
|
|
520
|
+
return /^\/(login|signup|otp|forgot-password|reset-password|invitation|unauthorized|auth\/)/.test(pathname);
|
|
521
|
+
}
|
|
522
|
+
function shouldAutoRedirectAfterLogin(routeName) {
|
|
523
|
+
if (ssoCallbackInProgress) return false;
|
|
524
|
+
if (routeName != null && POST_LOGIN_OWNER_ROUTES.has(String(routeName))) return false;
|
|
525
|
+
return true;
|
|
526
|
+
}
|
|
527
|
+
const callbackGate = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
528
|
+
__proto__: null,
|
|
529
|
+
POST_LOGIN_OWNER_ROUTES,
|
|
530
|
+
isAuthPublicPath,
|
|
531
|
+
isSsoCallbackInProgress,
|
|
532
|
+
setSsoCallbackInProgress,
|
|
533
|
+
shouldAutoRedirectAfterLogin
|
|
534
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
504
535
|
function getRedirectUrl(router2, config) {
|
|
505
536
|
const redirect2 = router2.currentRoute.value.query[config.queryKey];
|
|
506
537
|
return redirect2 || config.fallback;
|
|
@@ -527,6 +558,9 @@ function isValidRedirect(redirectUrl, allowedPaths) {
|
|
|
527
558
|
return true;
|
|
528
559
|
}
|
|
529
560
|
async function performRedirect(router2, config) {
|
|
561
|
+
if (!shouldAutoRedirectAfterLogin(router2.currentRoute.value.name)) {
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
530
564
|
const redirect2 = getRedirectUrl(router2, config);
|
|
531
565
|
console.log("[PerformRedirect] Current route query:", router2.currentRoute.value.query);
|
|
532
566
|
console.log("[PerformRedirect] Redirect URL:", redirect2);
|
|
@@ -1214,6 +1248,10 @@ function setupAutoRedirect() {
|
|
|
1214
1248
|
return;
|
|
1215
1249
|
}
|
|
1216
1250
|
const { performRedirect: performRedirect2 } = await Promise.resolve().then(() => redirect);
|
|
1251
|
+
const { shouldAutoRedirectAfterLogin: shouldAutoRedirectAfterLogin2 } = await Promise.resolve().then(() => callbackGate);
|
|
1252
|
+
if (!shouldAutoRedirectAfterLogin2(autoRedirectRouter.currentRoute.value.name)) {
|
|
1253
|
+
return;
|
|
1254
|
+
}
|
|
1217
1255
|
try {
|
|
1218
1256
|
await performRedirect2(autoRedirectRouter, redirectConfig);
|
|
1219
1257
|
} catch (error) {
|
|
@@ -1778,6 +1816,7 @@ exports.AuthState = AuthState;
|
|
|
1778
1816
|
exports.DEFAULT_AGENT_ID = DEFAULT_AGENT_ID;
|
|
1779
1817
|
exports.DEFAULT_REDIRECT_CONFIG = DEFAULT_REDIRECT_CONFIG;
|
|
1780
1818
|
exports.INTAKE_WORKFLOW_ID = INTAKE_WORKFLOW_ID;
|
|
1819
|
+
exports.POST_LOGIN_OWNER_ROUTES = POST_LOGIN_OWNER_ROUTES;
|
|
1781
1820
|
exports.PopupBlockedError = PopupBlockedError;
|
|
1782
1821
|
exports.PopupClosedError = PopupClosedError;
|
|
1783
1822
|
exports.PopupTimeoutError = PopupTimeoutError;
|
|
@@ -1795,7 +1834,9 @@ exports.getRedirectConfig = getRedirectConfig;
|
|
|
1795
1834
|
exports.getRedirectUrl = getRedirectUrl;
|
|
1796
1835
|
exports.getSSOProvider = getSSOProvider;
|
|
1797
1836
|
exports.handleSsoPopup = handleSsoPopup;
|
|
1837
|
+
exports.isAuthPublicPath = isAuthPublicPath;
|
|
1798
1838
|
exports.isMfaRequiredError = isMfaRequiredError;
|
|
1839
|
+
exports.isSsoCallbackInProgress = isSsoCallbackInProgress;
|
|
1799
1840
|
exports.isSupportedProvider = isSupportedProvider;
|
|
1800
1841
|
exports.isValidRedirect = isValidRedirect;
|
|
1801
1842
|
exports.mfaChallengeStep = mfaChallengeStep;
|
|
@@ -1807,6 +1848,8 @@ exports.providers = providers;
|
|
|
1807
1848
|
exports.resetAuthState = resetAuthState;
|
|
1808
1849
|
exports.resetInitialAuth = resetInitialAuth;
|
|
1809
1850
|
exports.setAuthContext = setAuthContext;
|
|
1851
|
+
exports.setSsoCallbackInProgress = setSsoCallbackInProgress;
|
|
1852
|
+
exports.shouldAutoRedirectAfterLogin = shouldAutoRedirectAfterLogin;
|
|
1810
1853
|
exports.sso = sso;
|
|
1811
1854
|
exports.ssoProvidersList = ssoProvidersList;
|
|
1812
1855
|
exports.useAuth = useAuth;
|
package/dist/index.d.ts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -499,6 +499,37 @@ function isMfaRequiredError(error) {
|
|
|
499
499
|
const code = ((_a = response == null ? void 0 : response.data) == null ? void 0 : _a.code) ?? ((_c = (_b = response == null ? void 0 : response.data) == null ? void 0 : _b.detail) == null ? void 0 : _c.code);
|
|
500
500
|
return (response == null ? void 0 : response.status) === 403 && (code === "mfa_required" || code === "mfa_enrollment_required");
|
|
501
501
|
}
|
|
502
|
+
const POST_LOGIN_OWNER_ROUTES = /* @__PURE__ */ new Set([
|
|
503
|
+
"auth-callback",
|
|
504
|
+
"AuthCallback",
|
|
505
|
+
"Callback",
|
|
506
|
+
// legacy route name in some apps
|
|
507
|
+
"auth-login-token",
|
|
508
|
+
"auth-mfa"
|
|
509
|
+
]);
|
|
510
|
+
let ssoCallbackInProgress = false;
|
|
511
|
+
function setSsoCallbackInProgress(value) {
|
|
512
|
+
ssoCallbackInProgress = value;
|
|
513
|
+
}
|
|
514
|
+
function isSsoCallbackInProgress() {
|
|
515
|
+
return ssoCallbackInProgress;
|
|
516
|
+
}
|
|
517
|
+
function isAuthPublicPath(pathname = typeof window !== "undefined" ? window.location.pathname : "") {
|
|
518
|
+
return /^\/(login|signup|otp|forgot-password|reset-password|invitation|unauthorized|auth\/)/.test(pathname);
|
|
519
|
+
}
|
|
520
|
+
function shouldAutoRedirectAfterLogin(routeName) {
|
|
521
|
+
if (ssoCallbackInProgress) return false;
|
|
522
|
+
if (routeName != null && POST_LOGIN_OWNER_ROUTES.has(String(routeName))) return false;
|
|
523
|
+
return true;
|
|
524
|
+
}
|
|
525
|
+
const callbackGate = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
526
|
+
__proto__: null,
|
|
527
|
+
POST_LOGIN_OWNER_ROUTES,
|
|
528
|
+
isAuthPublicPath,
|
|
529
|
+
isSsoCallbackInProgress,
|
|
530
|
+
setSsoCallbackInProgress,
|
|
531
|
+
shouldAutoRedirectAfterLogin
|
|
532
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
502
533
|
function getRedirectUrl(router2, config) {
|
|
503
534
|
const redirect2 = router2.currentRoute.value.query[config.queryKey];
|
|
504
535
|
return redirect2 || config.fallback;
|
|
@@ -525,6 +556,9 @@ function isValidRedirect(redirectUrl, allowedPaths) {
|
|
|
525
556
|
return true;
|
|
526
557
|
}
|
|
527
558
|
async function performRedirect(router2, config) {
|
|
559
|
+
if (!shouldAutoRedirectAfterLogin(router2.currentRoute.value.name)) {
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
528
562
|
const redirect2 = getRedirectUrl(router2, config);
|
|
529
563
|
console.log("[PerformRedirect] Current route query:", router2.currentRoute.value.query);
|
|
530
564
|
console.log("[PerformRedirect] Redirect URL:", redirect2);
|
|
@@ -1212,6 +1246,10 @@ function setupAutoRedirect() {
|
|
|
1212
1246
|
return;
|
|
1213
1247
|
}
|
|
1214
1248
|
const { performRedirect: performRedirect2 } = await Promise.resolve().then(() => redirect);
|
|
1249
|
+
const { shouldAutoRedirectAfterLogin: shouldAutoRedirectAfterLogin2 } = await Promise.resolve().then(() => callbackGate);
|
|
1250
|
+
if (!shouldAutoRedirectAfterLogin2(autoRedirectRouter.currentRoute.value.name)) {
|
|
1251
|
+
return;
|
|
1252
|
+
}
|
|
1215
1253
|
try {
|
|
1216
1254
|
await performRedirect2(autoRedirectRouter, redirectConfig);
|
|
1217
1255
|
} catch (error) {
|
|
@@ -1777,6 +1815,7 @@ export {
|
|
|
1777
1815
|
DEFAULT_AGENT_ID,
|
|
1778
1816
|
DEFAULT_REDIRECT_CONFIG,
|
|
1779
1817
|
INTAKE_WORKFLOW_ID,
|
|
1818
|
+
POST_LOGIN_OWNER_ROUTES,
|
|
1780
1819
|
PopupBlockedError,
|
|
1781
1820
|
PopupClosedError,
|
|
1782
1821
|
PopupTimeoutError,
|
|
@@ -1794,7 +1833,9 @@ export {
|
|
|
1794
1833
|
getRedirectUrl,
|
|
1795
1834
|
getSSOProvider,
|
|
1796
1835
|
handleSsoPopup,
|
|
1836
|
+
isAuthPublicPath,
|
|
1797
1837
|
isMfaRequiredError,
|
|
1838
|
+
isSsoCallbackInProgress,
|
|
1798
1839
|
isSupportedProvider,
|
|
1799
1840
|
isValidRedirect,
|
|
1800
1841
|
mfaChallengeStep,
|
|
@@ -1806,6 +1847,8 @@ export {
|
|
|
1806
1847
|
resetAuthState,
|
|
1807
1848
|
resetInitialAuth,
|
|
1808
1849
|
setAuthContext,
|
|
1850
|
+
setSsoCallbackInProgress,
|
|
1851
|
+
shouldAutoRedirectAfterLogin,
|
|
1809
1852
|
sso,
|
|
1810
1853
|
ssoProvidersList,
|
|
1811
1854
|
useAuth
|
package/package.json
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import {
|
|
3
|
+
isAuthPublicPath,
|
|
4
|
+
isSsoCallbackInProgress,
|
|
5
|
+
setSsoCallbackInProgress,
|
|
6
|
+
shouldAutoRedirectAfterLogin,
|
|
7
|
+
} from './callbackGate'
|
|
8
|
+
|
|
9
|
+
describe('callbackGate', () => {
|
|
10
|
+
test('tracks SSO callback progress', () => {
|
|
11
|
+
setSsoCallbackInProgress(false)
|
|
12
|
+
expect(isSsoCallbackInProgress()).toBe(false)
|
|
13
|
+
setSsoCallbackInProgress(true)
|
|
14
|
+
expect(isSsoCallbackInProgress()).toBe(true)
|
|
15
|
+
expect(shouldAutoRedirectAfterLogin('home')).toBe(false)
|
|
16
|
+
setSsoCallbackInProgress(false)
|
|
17
|
+
expect(shouldAutoRedirectAfterLogin('home')).toBe(true)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
test('skips soft redirect on post-login owner routes', () => {
|
|
21
|
+
setSsoCallbackInProgress(false)
|
|
22
|
+
expect(shouldAutoRedirectAfterLogin('auth-callback')).toBe(false)
|
|
23
|
+
expect(shouldAutoRedirectAfterLogin('AuthCallback')).toBe(false)
|
|
24
|
+
expect(shouldAutoRedirectAfterLogin('Callback')).toBe(false)
|
|
25
|
+
expect(shouldAutoRedirectAfterLogin('auth-mfa')).toBe(false)
|
|
26
|
+
expect(shouldAutoRedirectAfterLogin('auth-login')).toBe(true)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test('detects public auth paths', () => {
|
|
30
|
+
expect(isAuthPublicPath('/auth/callback')).toBe(true)
|
|
31
|
+
expect(isAuthPublicPath('/login')).toBe(true)
|
|
32
|
+
expect(isAuthPublicPath('/invitation/abc')).toBe(true)
|
|
33
|
+
expect(isAuthPublicPath('/')).toBe(false)
|
|
34
|
+
expect(isAuthPublicPath('/people')).toBe(false)
|
|
35
|
+
})
|
|
36
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gates post-login soft-navigation and expected-401 toast noise during SSO
|
|
3
|
+
* callback. The callback view owns a hard redirect (`location.replace`); any
|
|
4
|
+
* LOGIN → `router.push` race mounts the app shell early and flashes
|
|
5
|
+
* "Authentication required" from premature API calls.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Route names that perform their own post-login navigation. */
|
|
9
|
+
export const POST_LOGIN_OWNER_ROUTES = new Set([
|
|
10
|
+
'auth-callback',
|
|
11
|
+
'AuthCallback',
|
|
12
|
+
'Callback', // legacy route name in some apps
|
|
13
|
+
'auth-login-token',
|
|
14
|
+
'auth-mfa',
|
|
15
|
+
])
|
|
16
|
+
|
|
17
|
+
let ssoCallbackInProgress = false
|
|
18
|
+
|
|
19
|
+
export function setSsoCallbackInProgress(value: boolean): void {
|
|
20
|
+
ssoCallbackInProgress = value
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function isSsoCallbackInProgress(): boolean {
|
|
24
|
+
return ssoCallbackInProgress
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Public auth paths where a 401 is expected noise, not a user-facing failure. */
|
|
28
|
+
export function isAuthPublicPath(pathname = typeof window !== 'undefined' ? window.location.pathname : ''): boolean {
|
|
29
|
+
return /^\/(login|signup|otp|forgot-password|reset-password|invitation|unauthorized|auth\/)/.test(pathname)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Whether the default / host LOGIN redirect should run.
|
|
34
|
+
* Returns false during SSO callback processing and on routes that own redirect.
|
|
35
|
+
*/
|
|
36
|
+
export function shouldAutoRedirectAfterLogin(
|
|
37
|
+
routeName?: string | symbol | null,
|
|
38
|
+
): boolean {
|
|
39
|
+
if (ssoCallbackInProgress) return false
|
|
40
|
+
if (routeName != null && POST_LOGIN_OWNER_ROUTES.has(String(routeName))) return false
|
|
41
|
+
return true
|
|
42
|
+
}
|
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
|
@@ -261,6 +261,11 @@ function setupAutoRedirect() {
|
|
|
261
261
|
|
|
262
262
|
// Dynamic import to avoid circular dependency
|
|
263
263
|
const { performRedirect } = await import('./redirect')
|
|
264
|
+
const { shouldAutoRedirectAfterLogin } = await import('./callbackGate')
|
|
265
|
+
|
|
266
|
+
if (!shouldAutoRedirectAfterLogin(autoRedirectRouter.currentRoute.value.name)) {
|
|
267
|
+
return
|
|
268
|
+
}
|
|
264
269
|
|
|
265
270
|
try {
|
|
266
271
|
await performRedirect(autoRedirectRouter, redirectConfig!)
|