@bevel-software/platform-core-frontend 0.1.1 → 0.2.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/core/CoreAppShell.tsx +2 -0
- package/src/modules/admin/components/AccountsSection.tsx +151 -0
- package/src/modules/admin/components/AdminRolesPage.tsx +2 -0
- package/src/modules/auth/__tests__/AccountPage.test.tsx +77 -0
- package/src/modules/auth/__tests__/LoginScreen.test.tsx +82 -0
- package/src/modules/auth/components/AccountPage.tsx +113 -0
- package/src/modules/auth/components/LoginScreen.tsx +16 -14
- package/src/modules/auth/hooks/useAuthState.ts +3 -3
- package/src/modules/auth/services/account.api.ts +50 -0
- package/src/modules/auth/services/sso.ts +73 -0
- package/src/modules/toolbar/components/AdminMenu.tsx +11 -1
- package/src/modules/auth/services/microsoft-oauth.ts +0 -57
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bevel-software/platform-core-frontend",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Open-source core UI of the Bevel platform (raw TS/TSX source): workspace explorer/viewer, branches & change requests, Library, secrets, tools, access control.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"tailwind-merge": "^3.5.0",
|
|
39
39
|
"xlsx": "^0.18.5",
|
|
40
40
|
"yaml": "^2.9.0",
|
|
41
|
-
"@bevel-software/platform-shared": "0.
|
|
41
|
+
"@bevel-software/platform-shared": "0.2.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@testing-library/jest-dom": "^6.9.1",
|
|
@@ -40,6 +40,7 @@ import { ReviewProvider } from '../modules/review/state/ReviewProvider';
|
|
|
40
40
|
import { ReviewPanelSurface } from '../modules/review/components/ReviewPanelSurface';
|
|
41
41
|
import { ConnectToolsPage } from '../modules/secrets-vault/components/ConnectToolsPage';
|
|
42
42
|
import { SecretsPage } from '../modules/secrets-vault/components/SecretsPage';
|
|
43
|
+
import { AccountPage } from '../modules/auth/components/AccountPage';
|
|
43
44
|
import { ExternalAgentAccessPage } from '../modules/toolbar/components/ExternalAgentAccessPage';
|
|
44
45
|
import { AdminRolesPage } from '../modules/admin/components/AdminRolesPage';
|
|
45
46
|
import { ToolsExplorerPage } from '../modules/tools/ToolsExplorerPage';
|
|
@@ -298,6 +299,7 @@ export function ShellRoutes({ apps }: { apps: AppDef[] }) {
|
|
|
298
299
|
))}
|
|
299
300
|
<Route path="/connect" element={<ConnectToolsPage />} />
|
|
300
301
|
<Route path="/secrets" element={<SecretsPage />} />
|
|
302
|
+
<Route path="/account" element={<AccountPage />} />
|
|
301
303
|
<Route path="/external-agent-access" element={<ExternalAgentAccessPage />} />
|
|
302
304
|
<Route path="/roles-and-members" element={<AdminRolesPage />} />
|
|
303
305
|
<Route path="/tools" element={<ToolsExplorerPage />} />
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState, type FormEvent } from 'react';
|
|
2
|
+
import { Loader2 } from 'lucide-react';
|
|
3
|
+
import {
|
|
4
|
+
createAccount,
|
|
5
|
+
listAccounts,
|
|
6
|
+
type AccountSummary,
|
|
7
|
+
} from '../../auth/services/account.api';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The Accounts block of the Roles & Members page (admins only — the page
|
|
11
|
+
* itself gates): every known user, whether they can sign in with a password,
|
|
12
|
+
* and a create form. "Create" is an upsert by email — entering an existing
|
|
13
|
+
* account's email (re)sets that account's password, which doubles as the
|
|
14
|
+
* admin reset flow for a locked-out user.
|
|
15
|
+
*/
|
|
16
|
+
export function AccountsSection() {
|
|
17
|
+
const [accounts, setAccounts] = useState<AccountSummary[] | null>(null);
|
|
18
|
+
const [loadError, setLoadError] = useState<string | null>(null);
|
|
19
|
+
|
|
20
|
+
const [email, setEmail] = useState('');
|
|
21
|
+
const [name, setName] = useState('');
|
|
22
|
+
const [password, setPassword] = useState('');
|
|
23
|
+
const [formError, setFormError] = useState<string | null>(null);
|
|
24
|
+
const [created, setCreated] = useState<string | null>(null);
|
|
25
|
+
const [submitting, setSubmitting] = useState(false);
|
|
26
|
+
|
|
27
|
+
const reload = useCallback(() => {
|
|
28
|
+
listAccounts()
|
|
29
|
+
.then((rows) => {
|
|
30
|
+
setAccounts(rows);
|
|
31
|
+
setLoadError(null);
|
|
32
|
+
})
|
|
33
|
+
.catch((err) => setLoadError(err instanceof Error ? err.message : 'Could not load accounts'));
|
|
34
|
+
}, []);
|
|
35
|
+
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
reload();
|
|
38
|
+
}, [reload]);
|
|
39
|
+
|
|
40
|
+
async function handleSubmit(e: FormEvent<HTMLFormElement>) {
|
|
41
|
+
e.preventDefault();
|
|
42
|
+
setFormError(null);
|
|
43
|
+
setCreated(null);
|
|
44
|
+
setSubmitting(true);
|
|
45
|
+
try {
|
|
46
|
+
await createAccount(email.trim(), name.trim(), password);
|
|
47
|
+
setCreated(email.trim());
|
|
48
|
+
setEmail('');
|
|
49
|
+
setName('');
|
|
50
|
+
setPassword('');
|
|
51
|
+
reload();
|
|
52
|
+
} catch (err) {
|
|
53
|
+
setFormError(err instanceof Error ? err.message : 'Could not create account');
|
|
54
|
+
} finally {
|
|
55
|
+
setSubmitting(false);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<div className="mt-6 border-t border-slate-200 pt-4 space-y-4">
|
|
61
|
+
<div>
|
|
62
|
+
<h2 className="text-sm font-semibold text-slate-800">Accounts</h2>
|
|
63
|
+
<p className="text-xs text-slate-500 mt-0.5">
|
|
64
|
+
Who can sign in with a password. Creating an account with an existing
|
|
65
|
+
email resets that account's password.
|
|
66
|
+
</p>
|
|
67
|
+
</div>
|
|
68
|
+
|
|
69
|
+
{loadError ? (
|
|
70
|
+
<div className="p-3 text-sm text-red-700 bg-red-50 border border-red-200 rounded">
|
|
71
|
+
{loadError}
|
|
72
|
+
</div>
|
|
73
|
+
) : accounts === null ? (
|
|
74
|
+
<div className="flex items-center gap-2 p-2 text-sm text-slate-600">
|
|
75
|
+
<Loader2 size={14} className="animate-spin" /> Loading…
|
|
76
|
+
</div>
|
|
77
|
+
) : accounts.length === 0 ? (
|
|
78
|
+
<div className="p-2 text-sm text-slate-600">No accounts yet.</div>
|
|
79
|
+
) : (
|
|
80
|
+
<ul className="divide-y divide-slate-100 border border-slate-200 rounded-md">
|
|
81
|
+
{accounts.map((account) => (
|
|
82
|
+
<li key={account.id} className="flex items-center gap-3 px-3 py-2 text-sm">
|
|
83
|
+
<span className="font-medium text-slate-800">{account.name}</span>
|
|
84
|
+
<span className="text-slate-500 truncate">{account.email}</span>
|
|
85
|
+
<span className="ml-auto shrink-0 text-xs text-slate-500">
|
|
86
|
+
{account.hasPassword ? 'Password sign-in' : 'Single sign-on only'}
|
|
87
|
+
</span>
|
|
88
|
+
</li>
|
|
89
|
+
))}
|
|
90
|
+
</ul>
|
|
91
|
+
)}
|
|
92
|
+
|
|
93
|
+
<form onSubmit={handleSubmit} className="space-y-3 max-w-sm">
|
|
94
|
+
<h3 className="text-xs font-semibold uppercase tracking-wide text-slate-500">
|
|
95
|
+
Create account
|
|
96
|
+
</h3>
|
|
97
|
+
<label className="block space-y-1">
|
|
98
|
+
<span className="text-xs text-slate-600">Email</span>
|
|
99
|
+
<input
|
|
100
|
+
type="email"
|
|
101
|
+
required
|
|
102
|
+
value={email}
|
|
103
|
+
onChange={(e) => setEmail(e.target.value)}
|
|
104
|
+
className="w-full rounded-md bg-slate-100 border border-slate-300 px-3 py-2 text-sm text-slate-900 focus:outline-none focus:border-slate-400"
|
|
105
|
+
/>
|
|
106
|
+
</label>
|
|
107
|
+
<label className="block space-y-1">
|
|
108
|
+
<span className="text-xs text-slate-600">
|
|
109
|
+
Name <span className="text-slate-400">(optional)</span>
|
|
110
|
+
</span>
|
|
111
|
+
<input
|
|
112
|
+
type="text"
|
|
113
|
+
value={name}
|
|
114
|
+
onChange={(e) => setName(e.target.value)}
|
|
115
|
+
className="w-full rounded-md bg-slate-100 border border-slate-300 px-3 py-2 text-sm text-slate-900 focus:outline-none focus:border-slate-400"
|
|
116
|
+
/>
|
|
117
|
+
</label>
|
|
118
|
+
<label className="block space-y-1">
|
|
119
|
+
<span className="text-xs text-slate-600">Password</span>
|
|
120
|
+
<input
|
|
121
|
+
type="password"
|
|
122
|
+
required
|
|
123
|
+
autoComplete="new-password"
|
|
124
|
+
value={password}
|
|
125
|
+
onChange={(e) => setPassword(e.target.value)}
|
|
126
|
+
className="w-full rounded-md bg-slate-100 border border-slate-300 px-3 py-2 text-sm text-slate-900 focus:outline-none focus:border-slate-400"
|
|
127
|
+
/>
|
|
128
|
+
</label>
|
|
129
|
+
|
|
130
|
+
{formError && (
|
|
131
|
+
<div className="text-sm text-red-600" role="alert">
|
|
132
|
+
{formError}
|
|
133
|
+
</div>
|
|
134
|
+
)}
|
|
135
|
+
{created && (
|
|
136
|
+
<div className="text-sm text-emerald-700" role="status">
|
|
137
|
+
Account for {created} saved.
|
|
138
|
+
</div>
|
|
139
|
+
)}
|
|
140
|
+
|
|
141
|
+
<button
|
|
142
|
+
type="submit"
|
|
143
|
+
disabled={submitting}
|
|
144
|
+
className="rounded-md bg-bevel text-white text-sm font-medium px-4 py-2 hover:bg-bevel-deep disabled:opacity-60 disabled:cursor-not-allowed"
|
|
145
|
+
>
|
|
146
|
+
{submitting ? 'Saving…' : 'Create account'}
|
|
147
|
+
</button>
|
|
148
|
+
</form>
|
|
149
|
+
</div>
|
|
150
|
+
);
|
|
151
|
+
}
|
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
type RoleRosterEntry,
|
|
15
15
|
} from '../services/roles.api';
|
|
16
16
|
import { suggestPrincipals } from '../../access/api';
|
|
17
|
+
import { AccountsSection } from './AccountsSection';
|
|
17
18
|
|
|
18
19
|
/** The default-branch workspace id — roles are managed there (admin status derives from it). */
|
|
19
20
|
const ROLES_WORKSPACE_ID = encodeURIComponent(DEFAULT_BRANCH);
|
|
@@ -253,6 +254,7 @@ export function AdminRolesPage() {
|
|
|
253
254
|
))}
|
|
254
255
|
</div>
|
|
255
256
|
)}
|
|
257
|
+
<AccountsSection />
|
|
256
258
|
</PageShell>
|
|
257
259
|
);
|
|
258
260
|
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
import userEvent from '@testing-library/user-event';
|
|
4
|
+
import { AccountPage } from '../components/AccountPage';
|
|
5
|
+
import { AuthContext, type AuthContextValue } from '../state/auth.context';
|
|
6
|
+
import { changePassword } from '../services/account.api';
|
|
7
|
+
|
|
8
|
+
vi.mock('../services/account.api', () => ({
|
|
9
|
+
changePassword: vi.fn(),
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
function renderPage() {
|
|
13
|
+
const value: AuthContextValue = {
|
|
14
|
+
user: { id: 'u1', email: 'alice@example.com', name: 'Alice' },
|
|
15
|
+
token: 't',
|
|
16
|
+
isLoading: false,
|
|
17
|
+
login: vi.fn(async () => {}),
|
|
18
|
+
logout: vi.fn(),
|
|
19
|
+
};
|
|
20
|
+
return render(
|
|
21
|
+
<AuthContext.Provider value={value}>
|
|
22
|
+
<AccountPage />
|
|
23
|
+
</AuthContext.Provider>,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
vi.mocked(changePassword).mockReset();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('AccountPage', () => {
|
|
32
|
+
it('shows who is signed in', () => {
|
|
33
|
+
renderPage();
|
|
34
|
+
expect(screen.getByText('Alice')).toBeInTheDocument();
|
|
35
|
+
expect(screen.getByText('(alice@example.com)')).toBeInTheDocument();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('refuses mismatched confirmation without calling the API', async () => {
|
|
39
|
+
renderPage();
|
|
40
|
+
await userEvent.type(screen.getByLabelText(/New password/), 'new-password-1');
|
|
41
|
+
await userEvent.type(screen.getByLabelText(/Confirm new password/), 'different');
|
|
42
|
+
await userEvent.click(screen.getByRole('button', { name: 'Change password' }));
|
|
43
|
+
expect(screen.getByRole('alert')).toHaveTextContent('do not match');
|
|
44
|
+
expect(changePassword).not.toHaveBeenCalled();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('submits current + new password and reports success', async () => {
|
|
48
|
+
vi.mocked(changePassword).mockResolvedValue(undefined);
|
|
49
|
+
renderPage();
|
|
50
|
+
await userEvent.type(screen.getByLabelText(/Current password/), 'old-password');
|
|
51
|
+
await userEvent.type(screen.getByLabelText(/New password/), 'new-password-1');
|
|
52
|
+
await userEvent.type(screen.getByLabelText(/Confirm new password/), 'new-password-1');
|
|
53
|
+
await userEvent.click(screen.getByRole('button', { name: 'Change password' }));
|
|
54
|
+
expect(await screen.findByRole('status')).toHaveTextContent('Password changed.');
|
|
55
|
+
expect(changePassword).toHaveBeenCalledWith('old-password', 'new-password-1');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('omits the current password when left empty (SSO-only first set)', async () => {
|
|
59
|
+
vi.mocked(changePassword).mockResolvedValue(undefined);
|
|
60
|
+
renderPage();
|
|
61
|
+
await userEvent.type(screen.getByLabelText(/New password/), 'first-password');
|
|
62
|
+
await userEvent.type(screen.getByLabelText(/Confirm new password/), 'first-password');
|
|
63
|
+
await userEvent.click(screen.getByRole('button', { name: 'Change password' }));
|
|
64
|
+
expect(await screen.findByRole('status')).toHaveTextContent('Password changed.');
|
|
65
|
+
expect(changePassword).toHaveBeenCalledWith(undefined, 'first-password');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('surfaces API errors', async () => {
|
|
69
|
+
vi.mocked(changePassword).mockRejectedValue(new Error('Current password is incorrect'));
|
|
70
|
+
renderPage();
|
|
71
|
+
await userEvent.type(screen.getByLabelText(/Current password/), 'wrong');
|
|
72
|
+
await userEvent.type(screen.getByLabelText(/New password/), 'new-password-1');
|
|
73
|
+
await userEvent.type(screen.getByLabelText(/Confirm new password/), 'new-password-1');
|
|
74
|
+
await userEvent.click(screen.getByRole('button', { name: 'Change password' }));
|
|
75
|
+
expect(await screen.findByRole('alert')).toHaveTextContent('Current password is incorrect');
|
|
76
|
+
});
|
|
77
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { render, screen, waitFor } from '@testing-library/react';
|
|
3
|
+
import userEvent from '@testing-library/user-event';
|
|
4
|
+
import { LoginScreen } from '../components/LoginScreen';
|
|
5
|
+
import { AuthContext, type AuthContextValue } from '../state/auth.context';
|
|
6
|
+
import { fetchLoginProviders, startSsoLogin } from '../services/sso';
|
|
7
|
+
|
|
8
|
+
vi.mock('../services/sso', () => ({
|
|
9
|
+
fetchLoginProviders: vi.fn(),
|
|
10
|
+
startSsoLogin: vi.fn(),
|
|
11
|
+
OAUTH_ERROR_KEY: 'bevel_oauth_error',
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
const login = vi.fn(async () => {});
|
|
15
|
+
|
|
16
|
+
function renderScreen() {
|
|
17
|
+
const value: AuthContextValue = {
|
|
18
|
+
user: null,
|
|
19
|
+
token: null,
|
|
20
|
+
isLoading: false,
|
|
21
|
+
login,
|
|
22
|
+
logout: vi.fn(),
|
|
23
|
+
};
|
|
24
|
+
return render(
|
|
25
|
+
<AuthContext.Provider value={value}>
|
|
26
|
+
<LoginScreen />
|
|
27
|
+
</AuthContext.Provider>,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
beforeEach(() => {
|
|
32
|
+
vi.mocked(fetchLoginProviders).mockReset();
|
|
33
|
+
vi.mocked(startSsoLogin).mockReset();
|
|
34
|
+
login.mockClear();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('LoginScreen', () => {
|
|
38
|
+
it('renders the password form and one button per advertised SSO provider', async () => {
|
|
39
|
+
vi.mocked(fetchLoginProviders).mockResolvedValue({
|
|
40
|
+
password: true,
|
|
41
|
+
sso: [
|
|
42
|
+
{ key: 'oidc', label: 'Sign in with Okta', startPath: '/api/auth/oidc/login' },
|
|
43
|
+
{ key: 'microsoft', label: 'Sign in with Microsoft', startPath: '/api/auth/microsoft/login' },
|
|
44
|
+
],
|
|
45
|
+
});
|
|
46
|
+
renderScreen();
|
|
47
|
+
expect(screen.getByLabelText(/Email/)).toBeInTheDocument();
|
|
48
|
+
expect(screen.getByLabelText(/^Password$/)).toBeInTheDocument();
|
|
49
|
+
await waitFor(() => {
|
|
50
|
+
expect(screen.getByRole('button', { name: 'Sign in with Okta' })).toBeInTheDocument();
|
|
51
|
+
});
|
|
52
|
+
expect(screen.getByRole('button', { name: 'Sign in with Microsoft' })).toBeInTheDocument();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('starts the provider flow when its button is clicked', async () => {
|
|
56
|
+
const provider = { key: 'oidc', label: 'Sign in with Okta', startPath: '/api/auth/oidc/login' };
|
|
57
|
+
vi.mocked(fetchLoginProviders).mockResolvedValue({ password: true, sso: [provider] });
|
|
58
|
+
renderScreen();
|
|
59
|
+
await waitFor(() => screen.getByRole('button', { name: 'Sign in with Okta' }));
|
|
60
|
+
await userEvent.click(screen.getByRole('button', { name: 'Sign in with Okta' }));
|
|
61
|
+
expect(startSsoLogin).toHaveBeenCalledWith(provider);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('hides the password form when the probe disables it', async () => {
|
|
65
|
+
vi.mocked(fetchLoginProviders).mockResolvedValue({
|
|
66
|
+
password: false,
|
|
67
|
+
sso: [{ key: 'oidc', label: 'Single sign-on', startPath: '/api/auth/oidc/login' }],
|
|
68
|
+
});
|
|
69
|
+
renderScreen();
|
|
70
|
+
await waitFor(() => screen.getByRole('button', { name: 'Single sign-on' }));
|
|
71
|
+
expect(screen.queryByLabelText(/Email/)).not.toBeInTheDocument();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('submits email + password through the auth context', async () => {
|
|
75
|
+
vi.mocked(fetchLoginProviders).mockResolvedValue({ password: true, sso: [] });
|
|
76
|
+
renderScreen();
|
|
77
|
+
await userEvent.type(screen.getByLabelText(/Email/), 'alice@example.com');
|
|
78
|
+
await userEvent.type(screen.getByLabelText(/^Password$/), 'alices-password');
|
|
79
|
+
await userEvent.click(screen.getByRole('button', { name: 'Sign in' }));
|
|
80
|
+
expect(login).toHaveBeenCalledWith('alice@example.com', 'alices-password');
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { useState, type FormEvent } from 'react';
|
|
2
|
+
import { PageShell } from '../../../shared/components/PageShell';
|
|
3
|
+
import { useAuth } from '../state/auth.context';
|
|
4
|
+
import { changePassword } from '../services/account.api';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The standalone Account page (`/account`): who you're signed in as, and
|
|
8
|
+
* self-service password change. The current password is required once one is
|
|
9
|
+
* set; an account that only ever signed in via SSO sets its first password
|
|
10
|
+
* without one (the backend enforces both rules — the form always offers the
|
|
11
|
+
* field and simply sends it when filled).
|
|
12
|
+
*/
|
|
13
|
+
export function AccountPage() {
|
|
14
|
+
const { user } = useAuth();
|
|
15
|
+
const [currentPassword, setCurrentPassword] = useState('');
|
|
16
|
+
const [newPassword, setNewPassword] = useState('');
|
|
17
|
+
const [confirmPassword, setConfirmPassword] = useState('');
|
|
18
|
+
const [error, setError] = useState<string | null>(null);
|
|
19
|
+
const [saved, setSaved] = useState(false);
|
|
20
|
+
const [submitting, setSubmitting] = useState(false);
|
|
21
|
+
|
|
22
|
+
async function handleSubmit(e: FormEvent<HTMLFormElement>) {
|
|
23
|
+
e.preventDefault();
|
|
24
|
+
setError(null);
|
|
25
|
+
setSaved(false);
|
|
26
|
+
if (newPassword !== confirmPassword) {
|
|
27
|
+
setError('New passwords do not match');
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
setSubmitting(true);
|
|
31
|
+
try {
|
|
32
|
+
await changePassword(currentPassword || undefined, newPassword);
|
|
33
|
+
setSaved(true);
|
|
34
|
+
setCurrentPassword('');
|
|
35
|
+
setNewPassword('');
|
|
36
|
+
setConfirmPassword('');
|
|
37
|
+
} catch (err) {
|
|
38
|
+
setError(err instanceof Error ? err.message : 'Could not change password');
|
|
39
|
+
} finally {
|
|
40
|
+
setSubmitting(false);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<PageShell title="Account">
|
|
46
|
+
<div className="space-y-6">
|
|
47
|
+
{user && (
|
|
48
|
+
<div className="text-sm text-slate-700">
|
|
49
|
+
Signed in as <span className="font-medium">{user.name}</span>{' '}
|
|
50
|
+
<span className="text-slate-500">({user.email})</span>
|
|
51
|
+
</div>
|
|
52
|
+
)}
|
|
53
|
+
|
|
54
|
+
<form onSubmit={handleSubmit} className="space-y-3 max-w-sm">
|
|
55
|
+
<h2 className="text-sm font-semibold text-slate-800">Change password</h2>
|
|
56
|
+
<label className="block space-y-1">
|
|
57
|
+
<span className="text-xs text-slate-600">
|
|
58
|
+
Current password <span className="text-slate-400">(leave empty if you never set one)</span>
|
|
59
|
+
</span>
|
|
60
|
+
<input
|
|
61
|
+
type="password"
|
|
62
|
+
autoComplete="current-password"
|
|
63
|
+
value={currentPassword}
|
|
64
|
+
onChange={(e) => setCurrentPassword(e.target.value)}
|
|
65
|
+
className="w-full rounded-md bg-slate-100 border border-slate-300 px-3 py-2 text-sm text-slate-900 focus:outline-none focus:border-slate-400"
|
|
66
|
+
/>
|
|
67
|
+
</label>
|
|
68
|
+
<label className="block space-y-1">
|
|
69
|
+
<span className="text-xs text-slate-600">New password</span>
|
|
70
|
+
<input
|
|
71
|
+
type="password"
|
|
72
|
+
required
|
|
73
|
+
autoComplete="new-password"
|
|
74
|
+
value={newPassword}
|
|
75
|
+
onChange={(e) => setNewPassword(e.target.value)}
|
|
76
|
+
className="w-full rounded-md bg-slate-100 border border-slate-300 px-3 py-2 text-sm text-slate-900 focus:outline-none focus:border-slate-400"
|
|
77
|
+
/>
|
|
78
|
+
</label>
|
|
79
|
+
<label className="block space-y-1">
|
|
80
|
+
<span className="text-xs text-slate-600">Confirm new password</span>
|
|
81
|
+
<input
|
|
82
|
+
type="password"
|
|
83
|
+
required
|
|
84
|
+
autoComplete="new-password"
|
|
85
|
+
value={confirmPassword}
|
|
86
|
+
onChange={(e) => setConfirmPassword(e.target.value)}
|
|
87
|
+
className="w-full rounded-md bg-slate-100 border border-slate-300 px-3 py-2 text-sm text-slate-900 focus:outline-none focus:border-slate-400"
|
|
88
|
+
/>
|
|
89
|
+
</label>
|
|
90
|
+
|
|
91
|
+
{error && (
|
|
92
|
+
<div className="text-sm text-red-600" role="alert">
|
|
93
|
+
{error}
|
|
94
|
+
</div>
|
|
95
|
+
)}
|
|
96
|
+
{saved && (
|
|
97
|
+
<div className="text-sm text-emerald-700" role="status">
|
|
98
|
+
Password changed.
|
|
99
|
+
</div>
|
|
100
|
+
)}
|
|
101
|
+
|
|
102
|
+
<button
|
|
103
|
+
type="submit"
|
|
104
|
+
disabled={submitting}
|
|
105
|
+
className="rounded-md bg-bevel text-white text-sm font-medium px-4 py-2 hover:bg-bevel-deep disabled:opacity-60 disabled:cursor-not-allowed"
|
|
106
|
+
>
|
|
107
|
+
{submitting ? 'Saving…' : 'Change password'}
|
|
108
|
+
</button>
|
|
109
|
+
</form>
|
|
110
|
+
</div>
|
|
111
|
+
</PageShell>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { useState, useEffect, type FormEvent } from 'react';
|
|
2
2
|
import { useAuth } from '../state/auth.context';
|
|
3
3
|
import {
|
|
4
|
-
|
|
4
|
+
startSsoLogin,
|
|
5
5
|
fetchLoginProviders,
|
|
6
6
|
OAUTH_ERROR_KEY,
|
|
7
|
-
|
|
7
|
+
type SsoProvider,
|
|
8
|
+
} from '../services/sso';
|
|
8
9
|
|
|
9
10
|
const OAUTH_ERROR_MESSAGES: Record<string, string> = {
|
|
10
|
-
state: '
|
|
11
|
-
auth: '
|
|
12
|
-
start: 'Could not start
|
|
11
|
+
state: 'Sign-in could not be verified. Please try again.',
|
|
12
|
+
auth: 'Sign-in failed. Please try again.',
|
|
13
|
+
start: 'Could not start sign-in. Please try again.',
|
|
13
14
|
};
|
|
14
15
|
|
|
15
16
|
export function LoginScreen() {
|
|
@@ -21,23 +22,23 @@ export function LoginScreen() {
|
|
|
21
22
|
// Default to password-only until the probe resolves, matching fetchLoginProviders'
|
|
22
23
|
// failure fallback so we never flash a button the backend can't service.
|
|
23
24
|
const [passwordEnabled, setPasswordEnabled] = useState(true);
|
|
24
|
-
const [
|
|
25
|
+
const [ssoProviders, setSsoProviders] = useState<SsoProvider[]>([]);
|
|
25
26
|
|
|
26
|
-
// Surface an error returned by
|
|
27
|
+
// Surface an error returned by an SSO callback (stored by useAuthState),
|
|
27
28
|
// and probe which login methods this deployment offers.
|
|
28
29
|
useEffect(() => {
|
|
29
30
|
let mounted = true;
|
|
30
31
|
const oauthError = sessionStorage.getItem(OAUTH_ERROR_KEY);
|
|
31
32
|
if (oauthError) {
|
|
32
33
|
sessionStorage.removeItem(OAUTH_ERROR_KEY);
|
|
33
|
-
setError(OAUTH_ERROR_MESSAGES[oauthError] ?? '
|
|
34
|
+
setError(OAUTH_ERROR_MESSAGES[oauthError] ?? 'Sign-in failed.');
|
|
34
35
|
}
|
|
35
36
|
// Capability probe: never set state after unmount.
|
|
36
37
|
fetchLoginProviders()
|
|
37
38
|
.then((providers) => {
|
|
38
39
|
if (!mounted) return;
|
|
39
40
|
setPasswordEnabled(providers.password);
|
|
40
|
-
|
|
41
|
+
setSsoProviders(providers.sso);
|
|
41
42
|
})
|
|
42
43
|
.catch(() => {});
|
|
43
44
|
return () => {
|
|
@@ -112,7 +113,7 @@ export function LoginScreen() {
|
|
|
112
113
|
</div>
|
|
113
114
|
)}
|
|
114
115
|
|
|
115
|
-
{passwordEnabled &&
|
|
116
|
+
{passwordEnabled && ssoProviders.length > 0 && (
|
|
116
117
|
<div className="flex items-center gap-3 text-xs text-slate-400">
|
|
117
118
|
<span className="h-px flex-1 bg-slate-200" />
|
|
118
119
|
or
|
|
@@ -120,15 +121,16 @@ export function LoginScreen() {
|
|
|
120
121
|
</div>
|
|
121
122
|
)}
|
|
122
123
|
|
|
123
|
-
{
|
|
124
|
+
{ssoProviders.map((provider) => (
|
|
124
125
|
<button
|
|
126
|
+
key={provider.key}
|
|
125
127
|
type="button"
|
|
126
|
-
onClick={
|
|
128
|
+
onClick={() => startSsoLogin(provider)}
|
|
127
129
|
className="w-full rounded-md border border-slate-300 bg-white text-slate-700 text-sm font-medium py-2 hover:bg-slate-50"
|
|
128
130
|
>
|
|
129
|
-
|
|
131
|
+
{provider.label}
|
|
130
132
|
</button>
|
|
131
|
-
)}
|
|
133
|
+
))}
|
|
132
134
|
</form>
|
|
133
135
|
</div>
|
|
134
136
|
);
|
|
@@ -3,17 +3,17 @@ import type { AuthUser } from '@bevel-software/platform-shared';
|
|
|
3
3
|
import type { AuthContextValue } from '../state/auth.context';
|
|
4
4
|
import { loginWithPassword, fetchCurrentUser } from '../services/auth.api';
|
|
5
5
|
import { getToken, setToken, clearToken } from '../../../lib/api';
|
|
6
|
-
import {
|
|
6
|
+
import { consumeSsoCallback, OAUTH_ERROR_KEY } from '../services/sso';
|
|
7
7
|
|
|
8
8
|
export function useAuthState(): AuthContextValue {
|
|
9
9
|
const [user, setUser] = useState<AuthUser | null>(null);
|
|
10
10
|
const [token, setTokenState] = useState<string | null>(getToken());
|
|
11
11
|
const [isLoading, setIsLoading] = useState(true);
|
|
12
12
|
|
|
13
|
-
// On mount: first absorb
|
|
13
|
+
// On mount: first absorb an SSO OAuth callback (if we landed on one),
|
|
14
14
|
// then validate whatever token we have (from the callback or localStorage).
|
|
15
15
|
useEffect(() => {
|
|
16
|
-
const cb =
|
|
16
|
+
const cb = consumeSsoCallback();
|
|
17
17
|
if (cb.error) {
|
|
18
18
|
sessionStorage.setItem(OAUTH_ERROR_KEY, cb.error);
|
|
19
19
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { authFetch } from '../../../lib/api';
|
|
2
|
+
|
|
3
|
+
/** One row of the admin Accounts list (`GET /api/admin/accounts`). */
|
|
4
|
+
export interface AccountSummary {
|
|
5
|
+
id: string;
|
|
6
|
+
email: string;
|
|
7
|
+
name: string;
|
|
8
|
+
/** False for accounts that only ever signed in via SSO. */
|
|
9
|
+
hasPassword: boolean;
|
|
10
|
+
createdAt: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async function readError(res: Response, fallback: string): Promise<string> {
|
|
14
|
+
const body = await res.json().catch(() => ({}));
|
|
15
|
+
return (body as { error?: string }).error || fallback;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Self-service password change. `currentPassword` is required once one is set. */
|
|
19
|
+
export async function changePassword(
|
|
20
|
+
currentPassword: string | undefined,
|
|
21
|
+
newPassword: string,
|
|
22
|
+
): Promise<void> {
|
|
23
|
+
const res = await authFetch('/api/auth/change-password', {
|
|
24
|
+
method: 'POST',
|
|
25
|
+
headers: { 'Content-Type': 'application/json' },
|
|
26
|
+
body: JSON.stringify({ currentPassword, newPassword }),
|
|
27
|
+
});
|
|
28
|
+
if (!res.ok) throw new Error(await readError(res, 'Could not change password'));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function listAccounts(): Promise<AccountSummary[]> {
|
|
32
|
+
const res = await authFetch('/api/admin/accounts');
|
|
33
|
+
if (!res.ok) throw new Error(await readError(res, 'Could not load accounts'));
|
|
34
|
+
const body = (await res.json()) as { accounts: AccountSummary[] };
|
|
35
|
+
return body.accounts;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Create an account (or reset an existing account's password — deliberate upsert). */
|
|
39
|
+
export async function createAccount(
|
|
40
|
+
email: string,
|
|
41
|
+
name: string,
|
|
42
|
+
password: string,
|
|
43
|
+
): Promise<void> {
|
|
44
|
+
const res = await authFetch('/api/admin/accounts', {
|
|
45
|
+
method: 'POST',
|
|
46
|
+
headers: { 'Content-Type': 'application/json' },
|
|
47
|
+
body: JSON.stringify({ email, name: name || undefined, password }),
|
|
48
|
+
});
|
|
49
|
+
if (!res.ok) throw new Error(await readError(res, 'Could not create account'));
|
|
50
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSO client helpers, provider-agnostic: the login screen renders one button
|
|
3
|
+
* per provider advertised by the backend's capability probe (the generic OIDC
|
|
4
|
+
* plugin, the enterprise "Sign in with Microsoft", …).
|
|
5
|
+
*
|
|
6
|
+
* Every provider flow is a full-page redirect (not fetch): the button sends
|
|
7
|
+
* the browser to the provider's `startPath` on the backend, which bounces to
|
|
8
|
+
* the identity provider and back to `/auth/<key>/callback#token=…` (or
|
|
9
|
+
* `#error=…`). We read the JWT out of the URL fragment — fragments aren't
|
|
10
|
+
* sent to servers or stored in Referer/logs.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** Matches every provider's frontend callback path (`/auth/<key>/callback`). */
|
|
14
|
+
const CALLBACK_PATH_RE = /^\/auth\/[a-z0-9-]+\/callback$/;
|
|
15
|
+
|
|
16
|
+
/** Set when a callback carried an error, read once by the login screen. */
|
|
17
|
+
export const OAUTH_ERROR_KEY = 'bevel_oauth_error';
|
|
18
|
+
|
|
19
|
+
/** One SSO login method, as advertised by `GET /api/auth/providers`. */
|
|
20
|
+
export interface SsoProvider {
|
|
21
|
+
key: string;
|
|
22
|
+
label: string;
|
|
23
|
+
startPath: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Which login methods this deployment offers. */
|
|
27
|
+
export interface LoginProviders {
|
|
28
|
+
password: boolean;
|
|
29
|
+
sso: SsoProvider[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Kick off a provider's OAuth round-trip. */
|
|
33
|
+
export function startSsoLogin(provider: SsoProvider): void {
|
|
34
|
+
window.location.href = provider.startPath;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Probe which login methods are enabled. On failure default to password-only
|
|
39
|
+
* so a transient error never hides the only working method (and never
|
|
40
|
+
* surfaces an SSO button the backend can't service).
|
|
41
|
+
*/
|
|
42
|
+
export async function fetchLoginProviders(): Promise<LoginProviders> {
|
|
43
|
+
try {
|
|
44
|
+
const res = await fetch('/api/auth/providers');
|
|
45
|
+
if (!res.ok) return { password: true, sso: [] };
|
|
46
|
+
const body = (await res.json()) as Partial<LoginProviders>;
|
|
47
|
+
return {
|
|
48
|
+
password: body.password !== false,
|
|
49
|
+
sso: Array.isArray(body.sso) ? body.sso : [],
|
|
50
|
+
};
|
|
51
|
+
} catch {
|
|
52
|
+
return { password: true, sso: [] };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* If the current page is any provider's OAuth callback, extract the
|
|
58
|
+
* token/error from the fragment and scrub the URL (so the token never lingers
|
|
59
|
+
* in the address bar or browser history). Returns {} when not on a callback
|
|
60
|
+
* path.
|
|
61
|
+
*/
|
|
62
|
+
export function consumeSsoCallback(): { token?: string; error?: string } {
|
|
63
|
+
if (!CALLBACK_PATH_RE.test(window.location.pathname)) return {};
|
|
64
|
+
const raw = window.location.hash.startsWith('#')
|
|
65
|
+
? window.location.hash.slice(1)
|
|
66
|
+
: window.location.hash;
|
|
67
|
+
const params = new URLSearchParams(raw);
|
|
68
|
+
const token = params.get('token') ?? undefined;
|
|
69
|
+
const error = params.get('error') ?? undefined;
|
|
70
|
+
// Replace the callback URL with the app root so a reload doesn't re-process it.
|
|
71
|
+
window.history.replaceState({}, '', '/');
|
|
72
|
+
return { token, error };
|
|
73
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Fragment, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
-
import { Boxes, KeyRound, LibraryBig, Lock, Settings, Users } from 'lucide-react';
|
|
2
|
+
import { Boxes, CircleUserRound, KeyRound, LibraryBig, Lock, Settings, Users } from 'lucide-react';
|
|
3
3
|
import { useNavigate } from 'react-router-dom';
|
|
4
4
|
import { useAdmin } from '../../admin/state/admin.context';
|
|
5
5
|
import { useAppRegistry, type AdminMenuItem } from '../../../core/registry';
|
|
@@ -78,6 +78,16 @@ const CORE_MENU_ITEMS: AdminMenuItem[] = [
|
|
|
78
78
|
closeMenu();
|
|
79
79
|
},
|
|
80
80
|
},
|
|
81
|
+
{
|
|
82
|
+
id: 'account',
|
|
83
|
+
order: 90,
|
|
84
|
+
icon: <CircleUserRound size={14} />,
|
|
85
|
+
label: 'Account',
|
|
86
|
+
onSelect: ({ navigate, closeMenu }) => {
|
|
87
|
+
navigate('/account');
|
|
88
|
+
closeMenu();
|
|
89
|
+
},
|
|
90
|
+
},
|
|
81
91
|
{
|
|
82
92
|
id: 'roles-members',
|
|
83
93
|
section: 'admin',
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* "Sign in with Microsoft" client helpers.
|
|
3
|
-
*
|
|
4
|
-
* The flow is a full-page redirect (not fetch): the button sends the browser to
|
|
5
|
-
* the backend, which bounces to Microsoft and back to
|
|
6
|
-
* `/auth/microsoft/callback#token=…` (or `#error=…`). We read the JWT out of the
|
|
7
|
-
* URL fragment — fragments aren't sent to servers or stored in Referer/logs.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
const CALLBACK_PATH = '/auth/microsoft/callback';
|
|
11
|
-
/** Set when the callback carried an error, read once by the login screen. */
|
|
12
|
-
export const OAUTH_ERROR_KEY = 'bevel_oauth_error';
|
|
13
|
-
|
|
14
|
-
/** Kick off the OAuth round-trip. */
|
|
15
|
-
export function startMicrosoftLogin(): void {
|
|
16
|
-
window.location.href = '/api/auth/microsoft/login';
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/** Which login methods this deployment offers. */
|
|
20
|
-
export interface LoginProviders {
|
|
21
|
-
password: boolean;
|
|
22
|
-
microsoft: boolean;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Probe which login methods are enabled. On failure default to
|
|
27
|
-
* password-only so a transient error never hides the only working method
|
|
28
|
-
* (and never surfaces a Microsoft button the backend can't service).
|
|
29
|
-
*/
|
|
30
|
-
export async function fetchLoginProviders(): Promise<LoginProviders> {
|
|
31
|
-
try {
|
|
32
|
-
const res = await fetch('/api/auth/providers');
|
|
33
|
-
if (!res.ok) return { password: true, microsoft: false };
|
|
34
|
-
const body = (await res.json()) as Partial<LoginProviders>;
|
|
35
|
-
return { password: body.password !== false, microsoft: !!body.microsoft };
|
|
36
|
-
} catch {
|
|
37
|
-
return { password: true, microsoft: false };
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* If the current page is the OAuth callback, extract the token/error from the
|
|
43
|
-
* fragment and scrub the URL (so the token never lingers in the address bar or
|
|
44
|
-
* browser history). Returns {} when not on the callback path.
|
|
45
|
-
*/
|
|
46
|
-
export function consumeMicrosoftCallback(): { token?: string; error?: string } {
|
|
47
|
-
if (window.location.pathname !== CALLBACK_PATH) return {};
|
|
48
|
-
const raw = window.location.hash.startsWith('#')
|
|
49
|
-
? window.location.hash.slice(1)
|
|
50
|
-
: window.location.hash;
|
|
51
|
-
const params = new URLSearchParams(raw);
|
|
52
|
-
const token = params.get('token') ?? undefined;
|
|
53
|
-
const error = params.get('error') ?? undefined;
|
|
54
|
-
// Replace the callback URL with the app root so a reload doesn't re-process it.
|
|
55
|
-
window.history.replaceState({}, '', '/');
|
|
56
|
-
return { token, error };
|
|
57
|
-
}
|