@shellui/core 0.3.0 → 0.4.0
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/package.json +2 -2
- package/src/app.tsx +31 -3
- package/src/components/ui/dialog.tsx +26 -6
- package/src/components/ui/sonner.tsx +9 -5
- package/src/features/admin/AdminView.tsx +5 -49
- package/src/features/admin/config.ts +15 -0
- package/src/features/admin/index.ts +4 -0
- package/src/features/admin/routes.tsx +14 -0
- package/src/features/admin/utils.ts +79 -0
- package/src/features/auth/AuthProvider.tsx +68 -13
- package/src/features/auth/backends/shellui.ts +8 -1
- package/src/features/auth/components/AccessPendingView.tsx +112 -0
- package/src/features/auth/components/LoginButton.tsx +5 -10
- package/src/features/auth/components/LoginView.tsx +20 -2
- package/src/features/auth/components/OAuthCallbackView.tsx +211 -50
- package/src/features/auth/hooks/useAuth.tsx +8 -1
- package/src/features/auth/utils/authRequestError.spec.ts +52 -0
- package/src/features/auth/utils/authRequestError.ts +45 -0
- package/src/features/auth/utils/buildAuthUrlWithNext.spec.ts +18 -0
- package/src/features/auth/utils/buildAuthUrlWithNext.ts +12 -0
- package/src/features/auth/utils/index.ts +8 -0
- package/src/features/auth/utils/isTokenAutoRefreshDisabled.spec.ts +71 -0
- package/src/features/auth/utils/isTokenAutoRefreshDisabled.ts +18 -0
- package/src/features/config/ConfigProvider.ts +1 -1
- package/src/features/config/types.ts +46 -5
- package/src/features/layouts/AppLayout.tsx +3 -3
- package/src/features/layouts/OverlayShell.tsx +17 -28
- package/src/features/layouts/resolveSdkNavigatePath.spec.ts +55 -0
- package/src/features/layouts/resolveSdkNavigatePath.ts +68 -0
- package/src/features/settings/SettingsIcons.tsx +35 -0
- package/src/features/settings/SettingsProvider.tsx +79 -10
- package/src/features/settings/SettingsRoutes.tsx +14 -6
- package/src/features/settings/SettingsView.tsx +11 -5
- package/src/features/settings/components/Advanced.tsx +117 -44
- package/src/features/settings/components/Develop.tsx +38 -0
- package/src/features/settings/components/ServiceWorker.tsx +52 -25
- package/src/features/settings/components/Storage.tsx +138 -0
- package/src/features/settings/components/UpdateApp.tsx +1 -1
- package/src/features/settings/components/develop/StoragePickerTestButtons.tsx +148 -0
- package/src/features/settings/components/develop/UploadToastTestButton.tsx +29 -0
- package/src/features/settings/utils/buildSettingsForPropagation.spec.ts +55 -1
- package/src/features/settings/utils/buildSettingsForPropagation.ts +34 -0
- package/src/features/settings/utils/mergePreferencesIntoSettings.spec.ts +1 -1
- package/src/features/settings/utils/toSettingsUser.spec.ts +4 -0
- package/src/features/settings/utils/toSettingsUser.ts +2 -0
- package/src/features/sonner/SonnerContext.tsx +44 -60
- package/src/features/storage/StorageBridge.tsx +50 -0
- package/src/features/storage/StoragePickerContext.tsx +214 -0
- package/src/features/storage/api.spec.ts +303 -0
- package/src/features/storage/api.ts +414 -0
- package/src/features/storage/handleRequest.spec.ts +88 -0
- package/src/features/storage/handleRequest.ts +53 -0
- package/src/features/storage/quota.spec.ts +151 -0
- package/src/features/storage/quota.ts +104 -0
- package/src/features/storage/uploads/UploadToastCard.tsx +442 -0
- package/src/features/storage/uploads/UploadToaster.tsx +41 -0
- package/src/features/storage/uploads/handleTrackedUpload.spec.ts +170 -0
- package/src/features/storage/uploads/handleTrackedUpload.ts +97 -0
- package/src/features/storage/uploads/types.ts +24 -0
- package/src/features/storage/uploads/uploadQueue.spec.ts +118 -0
- package/src/features/storage/uploads/uploadQueue.ts +349 -0
- package/src/features/storage/uploads/useUploadQueue.ts +30 -0
- package/src/i18n/translations/en/common.json +33 -0
- package/src/i18n/translations/en/settings.json +56 -4
- package/src/i18n/translations/fr/common.json +33 -0
- package/src/i18n/translations/fr/settings.json +56 -4
- package/src/index.css +2 -1
- package/src/index.ts +2 -0
- package/src/lib/z-index.ts +9 -3
- package/src/routes/components/NavigationItemRoute.tsx +2 -1
- package/src/routes/routes.tsx +2 -13
- package/src/service-worker/register.ts +217 -101
- package/src/service-worker/sw-dev.ts +5 -1
- package/src/service-worker/sw.ts +5 -1
- package/src/shellui-target.d.ts +2 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { Button } from '../../../components/ui/button';
|
|
2
|
+
import { cn } from '../../../lib/utils';
|
|
3
|
+
|
|
4
|
+
type Props = {
|
|
5
|
+
message?: string | null;
|
|
6
|
+
code?: string | null;
|
|
7
|
+
onBackToLogin: () => void;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type AccessState = 'pending' | 'denied';
|
|
11
|
+
|
|
12
|
+
function resolveState(code: string | null | undefined): AccessState {
|
|
13
|
+
return code === 'access_denied' ? 'denied' : 'pending';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Shown when OAuth identity succeeded but company policy blocks tokens.
|
|
18
|
+
* Intentionally not framed as a technical failure.
|
|
19
|
+
*/
|
|
20
|
+
export const AccessPendingView = ({ message, code, onBackToLogin }: Props) => {
|
|
21
|
+
const state = resolveState(code);
|
|
22
|
+
const isDenied = state === 'denied';
|
|
23
|
+
|
|
24
|
+
const statusLabel = isDenied ? 'Organization policy' : 'Pending approval';
|
|
25
|
+
const title = isDenied
|
|
26
|
+
? 'Access is not available for your account'
|
|
27
|
+
: 'Your account is awaiting approval';
|
|
28
|
+
const lead = isDenied
|
|
29
|
+
? 'Your sign-in was verified, but this organization only admits users from approved email domains. Your domain is not on that list.'
|
|
30
|
+
: 'Your sign-in was verified. This organization requires an administrator to enable new accounts before you can continue.';
|
|
31
|
+
const nextSteps = isDenied
|
|
32
|
+
? 'An administrator has been notified. If you believe you should have access, contact your organization admin — they can enable your membership.'
|
|
33
|
+
: 'An administrator has been notified and will review your request. You will receive an email when access is enabled.';
|
|
34
|
+
|
|
35
|
+
const backendDetail = message?.trim() || null;
|
|
36
|
+
const detailLower = backendDetail?.toLowerCase() ?? '';
|
|
37
|
+
const showBackendDetail =
|
|
38
|
+
Boolean(backendDetail) &&
|
|
39
|
+
!detailLower.includes('waiting for an administrator') &&
|
|
40
|
+
!detailLower.includes('email domain is not authorized');
|
|
41
|
+
|
|
42
|
+
return (
|
|
43
|
+
<main className="flex min-h-full items-center justify-center bg-background px-6 py-12">
|
|
44
|
+
<div className="w-full max-w-xl animate-in fade-in-0 slide-in-from-bottom-3 duration-500">
|
|
45
|
+
<section
|
|
46
|
+
className="border border-border bg-card"
|
|
47
|
+
aria-labelledby="access-status-title"
|
|
48
|
+
>
|
|
49
|
+
<header className="border-b border-border px-8 py-6">
|
|
50
|
+
<div className="flex flex-wrap items-center gap-3">
|
|
51
|
+
<span
|
|
52
|
+
className={cn(
|
|
53
|
+
'inline-flex items-center gap-2 border px-2.5 py-1 font-mono text-[10px] font-medium uppercase tracking-[0.14em]',
|
|
54
|
+
isDenied
|
|
55
|
+
? 'border-foreground/25 bg-muted text-foreground'
|
|
56
|
+
: 'border-foreground/20 bg-background text-foreground',
|
|
57
|
+
)}
|
|
58
|
+
>
|
|
59
|
+
<span
|
|
60
|
+
className={cn(
|
|
61
|
+
'size-1.5 shrink-0 rounded-full',
|
|
62
|
+
isDenied ? 'bg-foreground/70' : 'bg-foreground',
|
|
63
|
+
)}
|
|
64
|
+
aria-hidden
|
|
65
|
+
/>
|
|
66
|
+
{statusLabel}
|
|
67
|
+
</span>
|
|
68
|
+
<span className="font-mono text-[10px] uppercase tracking-[0.14em] text-muted-foreground">
|
|
69
|
+
{isDenied ? 'Ref. access_denied' : 'Ref. access_pending'}
|
|
70
|
+
</span>
|
|
71
|
+
</div>
|
|
72
|
+
<h1
|
|
73
|
+
id="access-status-title"
|
|
74
|
+
className="mt-5 text-2xl font-semibold tracking-tight text-foreground md:text-[1.75rem]"
|
|
75
|
+
>
|
|
76
|
+
{title}
|
|
77
|
+
</h1>
|
|
78
|
+
</header>
|
|
79
|
+
|
|
80
|
+
<div className="space-y-5 px-8 py-6">
|
|
81
|
+
<p className="text-[15px] leading-relaxed text-foreground/90">{lead}</p>
|
|
82
|
+
|
|
83
|
+
<div className="border border-border bg-muted/30 px-4 py-3">
|
|
84
|
+
<p className="font-mono text-[10px] uppercase tracking-[0.12em] text-muted-foreground">
|
|
85
|
+
What happens next
|
|
86
|
+
</p>
|
|
87
|
+
<p className="mt-2 text-sm leading-relaxed text-foreground/85">{nextSteps}</p>
|
|
88
|
+
</div>
|
|
89
|
+
|
|
90
|
+
{showBackendDetail ? (
|
|
91
|
+
<p className="font-mono text-[11px] leading-relaxed text-muted-foreground">
|
|
92
|
+
Detail: {backendDetail}
|
|
93
|
+
</p>
|
|
94
|
+
) : null}
|
|
95
|
+
</div>
|
|
96
|
+
|
|
97
|
+
<footer className="flex flex-wrap items-center justify-between gap-3 border-t border-border px-8 py-5">
|
|
98
|
+
<p className="text-xs text-muted-foreground">You can close this page safely.</p>
|
|
99
|
+
<Button
|
|
100
|
+
type="button"
|
|
101
|
+
variant="outline"
|
|
102
|
+
size="sm"
|
|
103
|
+
onClick={onBackToLogin}
|
|
104
|
+
>
|
|
105
|
+
Back to login
|
|
106
|
+
</Button>
|
|
107
|
+
</footer>
|
|
108
|
+
</section>
|
|
109
|
+
</div>
|
|
110
|
+
</main>
|
|
111
|
+
);
|
|
112
|
+
};
|
|
@@ -7,6 +7,8 @@ import { cn } from '../../../lib/utils';
|
|
|
7
7
|
import { useAuth } from '../hooks/useAuth';
|
|
8
8
|
import { UserIcon } from '../../settings/components/UserIcon';
|
|
9
9
|
import { useConfig } from '../../config/useConfig';
|
|
10
|
+
import { getAdminPath } from '../../admin/config';
|
|
11
|
+
import { isAdminPath } from '../../admin/utils';
|
|
10
12
|
import { flattenNavigationItems, getNavPathPrefix } from '../../layouts/utils';
|
|
11
13
|
import { LogoutIcon, SidebarCaretIcon } from './LoginButtonIcons';
|
|
12
14
|
import {
|
|
@@ -169,17 +171,10 @@ export const LoginButton = ({
|
|
|
169
171
|
() => Boolean(user?.isStaff || user?.isCompanyOwner),
|
|
170
172
|
[user?.isCompanyOwner, user?.isStaff],
|
|
171
173
|
);
|
|
172
|
-
const
|
|
173
|
-
() => config.backend?.adminPathname?.trim() ?? null,
|
|
174
|
-
[config.backend?.adminPathname],
|
|
175
|
-
);
|
|
176
|
-
const adminPath =
|
|
177
|
-
configuredAdminPathname && configuredAdminPathname.startsWith('/')
|
|
178
|
-
? configuredAdminPathname
|
|
179
|
-
: urls.admin;
|
|
174
|
+
const adminPath = getAdminPath(config);
|
|
180
175
|
const isOnAdminRoute = useMemo(
|
|
181
|
-
() => location.pathname
|
|
182
|
-
[
|
|
176
|
+
() => isAdminPath(location.pathname, config),
|
|
177
|
+
[config, location.pathname],
|
|
183
178
|
);
|
|
184
179
|
|
|
185
180
|
const openAdminPanel = useCallback(() => {
|
|
@@ -9,13 +9,16 @@ import { LegalDocumentsLinks } from '../../legal/LegalDocumentsLinks';
|
|
|
9
9
|
import { useAuth } from '../hooks/useAuth';
|
|
10
10
|
import type { AuthSettings, LoginMethod } from '../types';
|
|
11
11
|
import {
|
|
12
|
+
buildAuthUrlWithNext,
|
|
12
13
|
formatProviderLabel,
|
|
13
14
|
getOAuthProviderCandidates,
|
|
14
15
|
getPreferredBackendProvider,
|
|
15
16
|
getProviderVisual,
|
|
17
|
+
isAccessPendingErrorCode,
|
|
16
18
|
isLoginMethod,
|
|
17
19
|
normalizeNextPath,
|
|
18
20
|
} from '../utils';
|
|
21
|
+
import { AccessPendingView } from './AccessPendingView';
|
|
19
22
|
|
|
20
23
|
const LAST_USED_LOGIN_STORAGE_KEY = 'shellui.auth.last_used_login';
|
|
21
24
|
|
|
@@ -112,6 +115,7 @@ export const LoginView = () => {
|
|
|
112
115
|
const [magicLinkError, setMagicLinkError] = useState<string | null>(null);
|
|
113
116
|
const [methodError, setMethodError] = useState<string | null>(null);
|
|
114
117
|
const [oauthBounceError, setOauthBounceError] = useState<string | null>(null);
|
|
118
|
+
const [oauthBounceCode, setOauthBounceCode] = useState<string | null>(null);
|
|
115
119
|
const [lastUsedLogin, setLastUsedLogin] = useState<LastUsedLogin | null>(
|
|
116
120
|
readLastUsedLoginFromStorage,
|
|
117
121
|
);
|
|
@@ -121,10 +125,10 @@ export const LoginView = () => {
|
|
|
121
125
|
return normalizeNextPath(params.get('next')) ?? '/';
|
|
122
126
|
}, [location.search]);
|
|
123
127
|
const loginPathWithNext = useMemo(() => {
|
|
124
|
-
return
|
|
128
|
+
return buildAuthUrlWithNext(urls.login, nextPath);
|
|
125
129
|
}, [nextPath]);
|
|
126
130
|
const oauthCallbackPathWithNext = useMemo(() => {
|
|
127
|
-
return
|
|
131
|
+
return buildAuthUrlWithNext(urls.loginCallback, nextPath);
|
|
128
132
|
}, [nextPath]);
|
|
129
133
|
|
|
130
134
|
useEffect(() => {
|
|
@@ -146,6 +150,7 @@ export const LoginView = () => {
|
|
|
146
150
|
? ` Add this origin in shellui-auth (Django admin or shellui-admin → Company → Login redirect URLs), for example: ${typeof window !== 'undefined' ? `${window.location.origin}/login` : '/login'}.`
|
|
147
151
|
: '';
|
|
148
152
|
setOauthBounceError(`${baseMsg}${hint}`);
|
|
153
|
+
setOauthBounceCode(code);
|
|
149
154
|
}, [location.hash, location.pathname, location.search, navigate]);
|
|
150
155
|
|
|
151
156
|
useEffect(() => {
|
|
@@ -384,6 +389,19 @@ export const LoginView = () => {
|
|
|
384
389
|
return null;
|
|
385
390
|
}
|
|
386
391
|
|
|
392
|
+
if (isAccessPendingErrorCode(oauthBounceCode)) {
|
|
393
|
+
return (
|
|
394
|
+
<AccessPendingView
|
|
395
|
+
message={oauthBounceError}
|
|
396
|
+
code={oauthBounceCode}
|
|
397
|
+
onBackToLogin={() => {
|
|
398
|
+
setOauthBounceError(null);
|
|
399
|
+
setOauthBounceCode(null);
|
|
400
|
+
}}
|
|
401
|
+
/>
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
|
|
387
405
|
return (
|
|
388
406
|
<main className="flex min-h-full w-full bg-background">
|
|
389
407
|
{!isIframeView && (
|
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
2
2
|
import { useLocation, useNavigate } from 'react-router';
|
|
3
3
|
import urls from '../../../constants/urls';
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
buildAuthUrlWithNext,
|
|
6
|
+
inferAccessPendingErrorCode,
|
|
7
|
+
isAccessPendingErrorCode,
|
|
8
|
+
normalizeNextPath,
|
|
9
|
+
} from '../utils';
|
|
5
10
|
import { useAuth } from '../hooks/useAuth';
|
|
11
|
+
import { AccessPendingView } from './AccessPendingView';
|
|
12
|
+
|
|
13
|
+
const SHELLUI_OAUTH_ERROR_PARAM = 'shellui_oauth_error';
|
|
14
|
+
const SHELLUI_OAUTH_ERROR_CODE_PARAM = 'shellui_oauth_error_code';
|
|
15
|
+
const PENDING_STORAGE_PREFIX = 'shellui.oauth.access_pending:';
|
|
6
16
|
|
|
7
17
|
const toPositiveInt = (raw: string | null): number | undefined => {
|
|
8
18
|
if (!raw) return undefined;
|
|
@@ -11,13 +21,69 @@ const toPositiveInt = (raw: string | null): number | undefined => {
|
|
|
11
21
|
return Math.trunc(n);
|
|
12
22
|
};
|
|
13
23
|
|
|
24
|
+
type PendingAccess = { message: string; code: string };
|
|
25
|
+
|
|
26
|
+
const readPendingForCode = (code: string): PendingAccess | null => {
|
|
27
|
+
if (typeof window === 'undefined') return null;
|
|
28
|
+
try {
|
|
29
|
+
const raw = sessionStorage.getItem(`${PENDING_STORAGE_PREFIX}${code}`);
|
|
30
|
+
if (!raw) return null;
|
|
31
|
+
const parsed = JSON.parse(raw) as { message?: unknown; code?: unknown };
|
|
32
|
+
if (typeof parsed.message === 'string' && typeof parsed.code === 'string') {
|
|
33
|
+
return { message: parsed.message, code: parsed.code };
|
|
34
|
+
}
|
|
35
|
+
} catch {
|
|
36
|
+
// ignore
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const writePendingForCode = (oauthCode: string, pending: PendingAccess) => {
|
|
42
|
+
if (typeof window === 'undefined') return;
|
|
43
|
+
try {
|
|
44
|
+
sessionStorage.setItem(`${PENDING_STORAGE_PREFIX}${oauthCode}`, JSON.stringify(pending));
|
|
45
|
+
} catch {
|
|
46
|
+
// ignore
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const clearPendingForCode = (oauthCode: string) => {
|
|
51
|
+
if (typeof window === 'undefined') return;
|
|
52
|
+
try {
|
|
53
|
+
sessionStorage.removeItem(`${PENDING_STORAGE_PREFIX}${oauthCode}`);
|
|
54
|
+
} catch {
|
|
55
|
+
// ignore
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const buildLoginPendingUrl = (nextPath: string, pending: PendingAccess) => {
|
|
60
|
+
const loginUrl = new URL(buildAuthUrlWithNext(urls.login, nextPath), window.location.origin);
|
|
61
|
+
loginUrl.searchParams.set(SHELLUI_OAUTH_ERROR_PARAM, pending.message.slice(0, 500));
|
|
62
|
+
loginUrl.searchParams.set(SHELLUI_OAUTH_ERROR_CODE_PARAM, pending.code);
|
|
63
|
+
return `${loginUrl.pathname}${loginUrl.search}`;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const looksLikeUsedOAuthCode = (message: string | null | undefined) => {
|
|
67
|
+
const text = (message || '').toLowerCase();
|
|
68
|
+
return (
|
|
69
|
+
text.includes('bad_verification_code') ||
|
|
70
|
+
text.includes('code has already been used') ||
|
|
71
|
+
text.includes('authorization code was already redeemed') ||
|
|
72
|
+
text.includes('invalid_grant') ||
|
|
73
|
+
text.includes('expired') ||
|
|
74
|
+
text.includes('redirect_uri')
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
|
|
14
78
|
export const OAuthCallbackView = () => {
|
|
15
79
|
const location = useLocation();
|
|
16
80
|
const navigate = useNavigate();
|
|
17
81
|
const { completeOAuthCallback, isAuthenticated } = useAuth();
|
|
18
82
|
const [localError, setLocalError] = useState<string | null>(null);
|
|
83
|
+
const [pendingAccess, setPendingAccess] = useState<PendingAccess | null>(null);
|
|
19
84
|
const [isWorking, setIsWorking] = useState(true);
|
|
20
|
-
|
|
85
|
+
/** OAuth `code` we already started exchanging (avoid double-spend / Strict Mode races). */
|
|
86
|
+
const startedCodeRef = useRef<string | null>(null);
|
|
21
87
|
const nextPath = useMemo(() => {
|
|
22
88
|
const params = new URLSearchParams(location.search);
|
|
23
89
|
return normalizeNextPath(params.get('next')) ?? '/';
|
|
@@ -29,46 +95,102 @@ export const OAuthCallbackView = () => {
|
|
|
29
95
|
}, [isAuthenticated, navigate, nextPath]);
|
|
30
96
|
|
|
31
97
|
useEffect(() => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
98
|
+
if (isAuthenticated) return;
|
|
99
|
+
|
|
100
|
+
const params = new URLSearchParams(location.search);
|
|
101
|
+
const oauthError = params.get('error');
|
|
102
|
+
if (oauthError) {
|
|
103
|
+
setLocalError(oauthError);
|
|
104
|
+
setIsWorking(false);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const code = params.get('code');
|
|
109
|
+
const provider = params.get('provider');
|
|
110
|
+
if (!code || !provider) {
|
|
111
|
+
setLocalError('Missing OAuth callback parameters.');
|
|
112
|
+
setIsWorking(false);
|
|
113
|
+
return;
|
|
37
114
|
}
|
|
38
|
-
|
|
115
|
+
|
|
116
|
+
// If a previous attempt already learned this login is pending, show it without re-exchanging.
|
|
117
|
+
const cachedPending = readPendingForCode(code);
|
|
118
|
+
if (cachedPending) {
|
|
119
|
+
setPendingAccess(cachedPending);
|
|
120
|
+
setIsWorking(false);
|
|
121
|
+
navigate(buildLoginPendingUrl(nextPath, cachedPending), { replace: true });
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (startedCodeRef.current === code) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
startedCodeRef.current = code;
|
|
129
|
+
|
|
130
|
+
let cancelled = false;
|
|
39
131
|
const run = async () => {
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
132
|
+
const redirectUri = `${window.location.origin}${location.pathname}${location.search}`;
|
|
133
|
+
let result: { ok: boolean; error?: string | null; errorCode?: string | null };
|
|
134
|
+
try {
|
|
135
|
+
result = await completeOAuthCallback({
|
|
136
|
+
provider,
|
|
137
|
+
code,
|
|
138
|
+
redirectUri,
|
|
139
|
+
oauthClientId: toPositiveInt(params.get('company_oauth_client_id')),
|
|
140
|
+
});
|
|
141
|
+
} catch (err) {
|
|
142
|
+
result = {
|
|
143
|
+
ok: false,
|
|
144
|
+
error: err instanceof Error ? err.message : 'Unable to complete OAuth login.',
|
|
145
|
+
errorCode: null,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Always apply UI state — do not drop failures when the effect cleans up
|
|
150
|
+
// (Strict Mode / callback identity changes), or the page stays on "Completing…".
|
|
151
|
+
if (!result.ok) {
|
|
152
|
+
const pendingCode =
|
|
153
|
+
(isAccessPendingErrorCode(result.errorCode) ? result.errorCode : null) ??
|
|
154
|
+
inferAccessPendingErrorCode(result.error);
|
|
155
|
+
|
|
156
|
+
if (pendingCode) {
|
|
157
|
+
const pending: PendingAccess = {
|
|
158
|
+
message:
|
|
159
|
+
(result.error && result.error.trim()) ||
|
|
160
|
+
'Your account was created and is waiting for an administrator to grant access.',
|
|
161
|
+
code: pendingCode,
|
|
162
|
+
};
|
|
163
|
+
writePendingForCode(code, pending);
|
|
164
|
+
setPendingAccess(pending);
|
|
45
165
|
setIsWorking(false);
|
|
166
|
+
if (!cancelled) {
|
|
167
|
+
navigate(buildLoginPendingUrl(nextPath, pending), { replace: true });
|
|
168
|
+
}
|
|
169
|
+
return;
|
|
46
170
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
if (!cancelled) {
|
|
53
|
-
setLocalError('Missing OAuth callback parameters.');
|
|
171
|
+
|
|
172
|
+
// Code was likely consumed by a cancelled first attempt that already marked pending.
|
|
173
|
+
const recovered = readPendingForCode(code);
|
|
174
|
+
if (recovered && looksLikeUsedOAuthCode(result.error)) {
|
|
175
|
+
setPendingAccess(recovered);
|
|
54
176
|
setIsWorking(false);
|
|
177
|
+
if (!cancelled) {
|
|
178
|
+
navigate(buildLoginPendingUrl(nextPath, recovered), { replace: true });
|
|
179
|
+
}
|
|
180
|
+
return;
|
|
55
181
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const redirectUri = `${window.location.origin}${location.pathname}${location.search}`;
|
|
59
|
-
const ok = await completeOAuthCallback({
|
|
60
|
-
provider,
|
|
61
|
-
code,
|
|
62
|
-
redirectUri,
|
|
63
|
-
oauthClientId: toPositiveInt(params.get('company_oauth_client_id')),
|
|
64
|
-
});
|
|
65
|
-
if (cancelled) return;
|
|
66
|
-
if (!ok) {
|
|
182
|
+
|
|
183
|
+
setLocalError(result.error ?? 'Unable to complete OAuth login.');
|
|
67
184
|
setIsWorking(false);
|
|
68
185
|
return;
|
|
69
186
|
}
|
|
70
|
-
|
|
187
|
+
|
|
188
|
+
clearPendingForCode(code);
|
|
189
|
+
if (!cancelled) {
|
|
190
|
+
navigate(nextPath, { replace: true });
|
|
191
|
+
}
|
|
71
192
|
};
|
|
193
|
+
|
|
72
194
|
void run();
|
|
73
195
|
return () => {
|
|
74
196
|
cancelled = true;
|
|
@@ -82,26 +204,65 @@ export const OAuthCallbackView = () => {
|
|
|
82
204
|
nextPath,
|
|
83
205
|
]);
|
|
84
206
|
|
|
85
|
-
const
|
|
86
|
-
|
|
207
|
+
const backToLogin = () => {
|
|
208
|
+
const params = new URLSearchParams(location.search);
|
|
209
|
+
const code = params.get('code');
|
|
210
|
+
if (code) clearPendingForCode(code);
|
|
211
|
+
navigate(buildAuthUrlWithNext(urls.login, nextPath), { replace: true });
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
if (pendingAccess) {
|
|
87
215
|
return (
|
|
88
|
-
<
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
216
|
+
<AccessPendingView
|
|
217
|
+
message={pendingAccess.message}
|
|
218
|
+
code={pendingAccess.code}
|
|
219
|
+
onBackToLogin={backToLogin}
|
|
220
|
+
/>
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (localError && !isWorking) {
|
|
225
|
+
return (
|
|
226
|
+
<main className="flex min-h-full items-center justify-center bg-background px-6 py-12">
|
|
227
|
+
<div className="w-full max-w-xl animate-in fade-in-0 slide-in-from-bottom-3 duration-500">
|
|
228
|
+
<section
|
|
229
|
+
className="border border-border bg-card"
|
|
230
|
+
aria-labelledby="oauth-error-title"
|
|
102
231
|
>
|
|
103
|
-
|
|
104
|
-
|
|
232
|
+
<header className="border-b border-border px-8 py-6">
|
|
233
|
+
<span className="inline-flex items-center gap-2 border border-destructive/30 bg-destructive/5 px-2.5 py-1 font-mono text-[10px] font-medium uppercase tracking-[0.14em] text-destructive">
|
|
234
|
+
<span
|
|
235
|
+
className="size-1.5 shrink-0 rounded-full bg-destructive"
|
|
236
|
+
aria-hidden
|
|
237
|
+
/>
|
|
238
|
+
Sign-in error
|
|
239
|
+
</span>
|
|
240
|
+
<h1
|
|
241
|
+
id="oauth-error-title"
|
|
242
|
+
className="mt-5 text-2xl font-semibold tracking-tight text-foreground"
|
|
243
|
+
>
|
|
244
|
+
We could not complete sign-in
|
|
245
|
+
</h1>
|
|
246
|
+
</header>
|
|
247
|
+
<div className="space-y-4 px-8 py-6">
|
|
248
|
+
<p className="text-[15px] leading-relaxed text-foreground/90">
|
|
249
|
+
Something went wrong while connecting to your identity provider. This is a technical
|
|
250
|
+
issue, not an account approval status.
|
|
251
|
+
</p>
|
|
252
|
+
<p className="border border-border bg-muted/30 px-4 py-3 font-mono text-xs leading-relaxed text-muted-foreground">
|
|
253
|
+
{localError}
|
|
254
|
+
</p>
|
|
255
|
+
</div>
|
|
256
|
+
<footer className="flex justify-end border-t border-border px-8 py-5">
|
|
257
|
+
<button
|
|
258
|
+
type="button"
|
|
259
|
+
className="text-sm font-medium text-primary underline-offset-4 hover:underline"
|
|
260
|
+
onClick={backToLogin}
|
|
261
|
+
>
|
|
262
|
+
Back to login
|
|
263
|
+
</button>
|
|
264
|
+
</footer>
|
|
265
|
+
</section>
|
|
105
266
|
</div>
|
|
106
267
|
</main>
|
|
107
268
|
);
|
|
@@ -3,12 +3,19 @@ import type { AuthEvent, AuthSession, AuthSettings, AuthUser, UserPreferences }
|
|
|
3
3
|
|
|
4
4
|
export type { AuthSession, AuthUser } from '../types';
|
|
5
5
|
|
|
6
|
+
export interface OAuthCallbackResult {
|
|
7
|
+
ok: boolean;
|
|
8
|
+
error?: string | null;
|
|
9
|
+
errorCode?: string | null;
|
|
10
|
+
}
|
|
11
|
+
|
|
6
12
|
export interface AuthContextValue {
|
|
7
13
|
session: AuthSession | null;
|
|
8
14
|
user: AuthUser | null;
|
|
9
15
|
isAuthenticated: boolean;
|
|
10
16
|
isLoading: boolean;
|
|
11
17
|
error: string | null;
|
|
18
|
+
errorCode: string | null;
|
|
12
19
|
authEvent: AuthEvent;
|
|
13
20
|
clearAuthEvent: () => void;
|
|
14
21
|
completeOAuthCallback: (params: {
|
|
@@ -16,7 +23,7 @@ export interface AuthContextValue {
|
|
|
16
23
|
code: string;
|
|
17
24
|
redirectUri: string;
|
|
18
25
|
oauthClientId?: number;
|
|
19
|
-
}) => Promise<
|
|
26
|
+
}) => Promise<OAuthCallbackResult>;
|
|
20
27
|
startOAuth: (provider: string, redirectPath?: string, oauthClientId?: number) => boolean;
|
|
21
28
|
startWeb3Ethereum: () => Promise<boolean>;
|
|
22
29
|
getAuthSettings: () => Promise<AuthSettings>;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
AuthRequestError,
|
|
4
|
+
getAuthRequestErrorCode,
|
|
5
|
+
inferAccessPendingErrorCode,
|
|
6
|
+
isAccessPendingErrorCode,
|
|
7
|
+
} from './authRequestError';
|
|
8
|
+
|
|
9
|
+
describe('isAccessPendingErrorCode', () => {
|
|
10
|
+
it('recognizes pending and denied codes', () => {
|
|
11
|
+
expect(isAccessPendingErrorCode('access_pending')).toBe(true);
|
|
12
|
+
expect(isAccessPendingErrorCode('access_denied')).toBe(true);
|
|
13
|
+
expect(isAccessPendingErrorCode('oauth_authorize_failed')).toBe(false);
|
|
14
|
+
expect(isAccessPendingErrorCode(null)).toBe(false);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe('AuthRequestError', () => {
|
|
19
|
+
it('stores optional error code', () => {
|
|
20
|
+
const err = new AuthRequestError('Waiting for approval.', 'access_pending');
|
|
21
|
+
expect(err.message).toBe('Waiting for approval.');
|
|
22
|
+
expect(err.code).toBe('access_pending');
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe('getAuthRequestErrorCode', () => {
|
|
27
|
+
it('reads code from AuthRequestError and duck-typed objects', () => {
|
|
28
|
+
expect(getAuthRequestErrorCode(new AuthRequestError('x', 'access_pending'))).toBe(
|
|
29
|
+
'access_pending',
|
|
30
|
+
);
|
|
31
|
+
expect(getAuthRequestErrorCode({ name: 'AuthRequestError', code: 'access_denied' })).toBe(
|
|
32
|
+
'access_denied',
|
|
33
|
+
);
|
|
34
|
+
expect(getAuthRequestErrorCode(new Error('nope'))).toBe(null);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('inferAccessPendingErrorCode', () => {
|
|
39
|
+
it('infers from known backend messages', () => {
|
|
40
|
+
expect(
|
|
41
|
+
inferAccessPendingErrorCode(
|
|
42
|
+
'Your account was created and is waiting for an administrator to grant access.',
|
|
43
|
+
),
|
|
44
|
+
).toBe('access_pending');
|
|
45
|
+
expect(
|
|
46
|
+
inferAccessPendingErrorCode(
|
|
47
|
+
'Your email domain is not authorized for this company. An administrator has been notified.',
|
|
48
|
+
),
|
|
49
|
+
).toBe('access_denied');
|
|
50
|
+
expect(inferAccessPendingErrorCode('Invalid code')).toBe(null);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export class AuthRequestError extends Error {
|
|
2
|
+
readonly code: string | null;
|
|
3
|
+
|
|
4
|
+
constructor(message: string, code?: string | null) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = 'AuthRequestError';
|
|
7
|
+
this.code = typeof code === 'string' && code.trim() ? code.trim() : null;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Company join blocked until an admin enables the account (or domain mismatch). */
|
|
12
|
+
export function isAccessPendingErrorCode(code: string | null | undefined): boolean {
|
|
13
|
+
return code === 'access_pending' || code === 'access_denied';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Duck-type safe: `instanceof` can fail across duplicated bundles. */
|
|
17
|
+
export function getAuthRequestErrorCode(err: unknown): string | null {
|
|
18
|
+
if (!err || typeof err !== 'object') return null;
|
|
19
|
+
if (err instanceof AuthRequestError) return err.code;
|
|
20
|
+
const maybe = err as { name?: unknown; code?: unknown };
|
|
21
|
+
if (maybe.name === 'AuthRequestError' && typeof maybe.code === 'string' && maybe.code.trim()) {
|
|
22
|
+
return maybe.code.trim();
|
|
23
|
+
}
|
|
24
|
+
if (typeof maybe.code === 'string' && maybe.code.trim()) {
|
|
25
|
+
return maybe.code.trim();
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Infer pending/denied from known backend messages when error_code is missing. */
|
|
31
|
+
export function inferAccessPendingErrorCode(
|
|
32
|
+
message: string | null | undefined,
|
|
33
|
+
): 'access_pending' | 'access_denied' | null {
|
|
34
|
+
const text = (message || '').toLowerCase();
|
|
35
|
+
if (!text) return null;
|
|
36
|
+
if (text.includes('email domain is not authorized')) return 'access_denied';
|
|
37
|
+
if (
|
|
38
|
+
text.includes('waiting for an administrator') ||
|
|
39
|
+
text.includes('company access is disabled') ||
|
|
40
|
+
text.includes('grant access')
|
|
41
|
+
) {
|
|
42
|
+
return 'access_pending';
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import urls from '../../../constants/urls';
|
|
3
|
+
import { buildAuthUrlWithNext } from './buildAuthUrlWithNext';
|
|
4
|
+
|
|
5
|
+
describe('buildAuthUrlWithNext', () => {
|
|
6
|
+
it('returns the base path when next is null, undefined, or root', () => {
|
|
7
|
+
expect(buildAuthUrlWithNext(urls.login, null)).toBe(urls.login);
|
|
8
|
+
expect(buildAuthUrlWithNext(urls.login, undefined)).toBe(urls.login);
|
|
9
|
+
expect(buildAuthUrlWithNext(urls.login, '/')).toBe(urls.login);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('appends next when it points to a non-root path', () => {
|
|
13
|
+
expect(buildAuthUrlWithNext(urls.login, '/dashboard')).toBe(`${urls.login}?next=%2Fdashboard`);
|
|
14
|
+
expect(buildAuthUrlWithNext(urls.loginCallback, '/settings/user')).toBe(
|
|
15
|
+
`${urls.loginCallback}?next=%2Fsettings%2Fuser`,
|
|
16
|
+
);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const shouldIncludeNextParam = (nextPath: string | null | undefined): nextPath is string =>
|
|
2
|
+
Boolean(nextPath && nextPath !== '/');
|
|
3
|
+
|
|
4
|
+
export const buildAuthUrlWithNext = (
|
|
5
|
+
basePath: string,
|
|
6
|
+
nextPath: string | null | undefined,
|
|
7
|
+
): string => {
|
|
8
|
+
if (!shouldIncludeNextParam(nextPath)) {
|
|
9
|
+
return basePath;
|
|
10
|
+
}
|
|
11
|
+
return `${basePath}?next=${encodeURIComponent(nextPath)}`;
|
|
12
|
+
};
|