diff --git a/hifi-src/src/App.jsx b/hifi-src/src/App.jsx index 392a688..26f8ce1 100644 --- a/hifi-src/src/App.jsx +++ b/hifi-src/src/App.jsx @@ -61,6 +61,7 @@ const Datenschutz = lazyPage(() => import('./pages/public/Datenschutz.jsx')); const AGB = lazyPage(() => import('./pages/public/AGB.jsx')); const SetupWizard = lazyPage(() => import('./pages/public/SetupWizard.jsx')); const Login = lazyPage(() => import('./pages/admin/Login.jsx')); +const ConfirmEmail = lazyPage(() => import('./pages/admin/ConfirmEmail.jsx')); const Dashboard = lazyPage(() => import('./pages/admin/Dashboard.jsx')); const Brands = lazyPage(() => import('./pages/admin/Brands.jsx')); const Models = lazyPage(() => import('./pages/admin/Models.jsx')); @@ -130,6 +131,7 @@ export default function App() { } /> + } /> }> }> diff --git a/hifi-src/src/context/AuthContext.jsx b/hifi-src/src/context/AuthContext.jsx index ff959ce..89e1bfa 100644 --- a/hifi-src/src/context/AuthContext.jsx +++ b/hifi-src/src/context/AuthContext.jsx @@ -18,7 +18,7 @@ export function AuthProvider({ children }) { const login = async (username, password) => { const data = await api.post('/auth/login', { username, password }); if (data.requires_2fa) { - return { requires2fa: true }; + return { requires2fa: true, method: data.method || 'totp', emailHint: data.email_hint || '' }; } setUser(data); return { requires2fa: false }; @@ -29,6 +29,8 @@ export function AuthProvider({ children }) { setUser(data); }; + const resendLoginCode = () => api.post('/auth/resend-code', {}); + const logout = async () => { await api.post('/auth/logout', {}); setUser(null); @@ -43,7 +45,7 @@ export function AuthProvider({ children }) { !!user && (user.is_super_admin || user.permissions?.includes(permission)); return ( - + {children} ); diff --git a/hifi-src/src/pages/admin/AccountSettings.jsx b/hifi-src/src/pages/admin/AccountSettings.jsx index 8bd7b5b..5692626 100644 --- a/hifi-src/src/pages/admin/AccountSettings.jsx +++ b/hifi-src/src/pages/admin/AccountSettings.jsx @@ -10,6 +10,159 @@ const emptyPasswordForm = { two_factor_code: '', }; +const inputClass = 'w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-900'; + +function EmailCard({ user, refreshUser }) { + const [phase, setPhase] = useState('idle'); // idle | edit | remove + const [email, setEmail] = useState(''); + const [password, setPassword] = useState(''); + const [error, setError] = useState(''); + const [notice, setNotice] = useState(''); + const [busy, setBusy] = useState(false); + + const run = async (fn, successText) => { + setError(''); + setNotice(''); + setBusy(true); + try { + await fn(); + await refreshUser(); + setPhase('idle'); + setPassword(''); + if (successText) setNotice(successText); + } catch (err) { + setError(err.message); + } finally { + setBusy(false); + } + }; + + const submitEmail = (e) => { + e.preventDefault(); + run( + () => api.post('/auth/email', { email, password }), + `Bestätigungslink an ${email} gesendet. Die Adresse gilt erst nach dem Klick darauf.`, + ); + }; + + const submitRemove = (e) => { + e.preventDefault(); + run(() => api.post('/auth/email/remove', { password }), 'E-Mail-Adresse entfernt.'); + }; + + const cancel = () => { setPhase('idle'); setPassword(''); setError(''); }; + + return ( +
+

E-Mail-Adresse

+

+ {user?.two_factor_enabled + ? 'Da die Authenticator-App aktiv ist, wird beim Login sie abgefragt, nicht die E-Mail.' + : 'Mit bestätigter Adresse bekommst du beim Login einen Code per Mail, den du zusätzlich zum Passwort eingibst.'} +

+ + {phase === 'idle' && ( + <> +

+ {user?.email && user?.email_verified ? ( + <> + {user.email}{' '} + · bestätigt + + ) : ( + Keine E-Mail-Adresse hinterlegt. + )} +

+ {user?.pending_email && ( +
+ {user.pending_email} wartet auf Bestätigung. Klick auf den Link in der Mail. +
+ + +
+
+ )} + {notice &&

{notice}

} + {error &&

{error}

} +
+ + {(user?.email || user?.pending_email) && ( + + )} +
+ + )} + + {phase === 'edit' && ( +
+
+ + setEmail(e.target.value)} required autoFocus className={inputClass} /> +
+
+ + setPassword(e.target.value)} required className={inputClass} /> +
+ {error &&

{error}

} +
+ + +
+
+ )} + + {phase === 'remove' && ( +
+

+ Ohne E-Mail-Adresse (und ohne Authenticator-App) reicht beim Login wieder das Passwort. +

+
+ + setPassword(e.target.value)} required autoFocus className={inputClass} /> +
+ {error &&

{error}

} +
+ + +
+
+ )} +
+ ); +} + export default function AccountSettings() { const { user, refreshUser } = useAuth(); const [phase, setPhase] = useState('idle'); // idle | setup | recovery-codes | disable | regenerate @@ -124,6 +277,8 @@ export default function AccountSettings() { Angemeldet als {user?.username}{user?.is_super_admin ? ' (Super-Admin)' : ''}.

+ +

Passwort ändern

diff --git a/hifi-src/src/pages/admin/AdminUsers.jsx b/hifi-src/src/pages/admin/AdminUsers.jsx index 66e883b..9156dee 100644 --- a/hifi-src/src/pages/admin/AdminUsers.jsx +++ b/hifi-src/src/pages/admin/AdminUsers.jsx @@ -3,7 +3,21 @@ import { api } from '../../api/client.js'; import { useAuth } from '../../context/AuthContext.jsx'; import { useToast, useUnsavedChanges } from '../../context/AdminUiContext.jsx'; -const emptyForm = { username: '', password: '', is_super_admin: false, permissions: [], group_ids: [] }; +const emptyForm = { username: '', email: '', password: '', is_super_admin: false, permissions: [], group_ids: [] }; + +function EmailStatus({ user }) { + if (user.pending_email) { + return ( + + {user.pending_email} · unbestätigt + + ); + } + if (user.email && user.email_verified) { + return {user.email}; + } + return null; +} function formatLastActive(value) { if (!value) return 'Noch nie'; @@ -24,6 +38,7 @@ export default function AdminUsers() { const [form, setForm] = useState(emptyForm); const [editingId, setEditingId] = useState(null); const [editingIsPrimary, setEditingIsPrimary] = useState(false); + const [editingUser, setEditingUser] = useState(null); const [error, setError] = useState(''); const [savedForm, setSavedForm] = useState(emptyForm); const formRef = useRef(null); @@ -44,8 +59,10 @@ export default function AdminUsers() { const startEdit = (u) => { setEditingId(u.id); setEditingIsPrimary(u.is_primary); + setEditingUser(u); const next = { username: u.username, + email: u.pending_email || u.email || '', password: '', is_super_admin: u.is_super_admin, permissions: u.permissions, @@ -59,6 +76,7 @@ export default function AdminUsers() { const resetForm = () => { setEditingId(null); setEditingIsPrimary(false); + setEditingUser(null); setForm(emptyForm); setSavedForm(emptyForm); }; @@ -81,16 +99,18 @@ export default function AdminUsers() { e.preventDefault(); setError(''); try { + let result; if (editingId) { - await api.put(`/admin-users/${editingId}`, form); + result = await api.put(`/admin-users/${editingId}`, form); } else { if (form.password.length < 8) { setError('Passwort muss mindestens 8 Zeichen lang sein'); return; } - await api.post('/admin-users', form); + result = await api.post('/admin-users', form); } toast.success(editingId ? 'Benutzer gespeichert.' : `Benutzer „${form.username}“ angelegt.`); + if (result?.email_notice) toast.info(result.email_notice); resetForm(); load(); } catch (err) { @@ -135,6 +155,7 @@ export default function AdminUsers() { {u.username} {u.id === currentUser?.id && (du)} {u.is_primary && (Erst-Administrator)} + {u.is_super_admin ? ( @@ -185,6 +206,21 @@ export default function AdminUsers() { className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-900" />

+
+ + setForm({ ...form, email: e.target.value })} + placeholder="optional" + className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-900" + /> +

+ {editingUser?.pending_email && form.email === editingUser.pending_email + ? 'Wartet auf Bestätigung. Erst nach Klick auf den Link in der Mail gilt die Adresse.' + : 'Eine neue Adresse bekommt zuerst einen Bestätigungslink. Danach geht beim Login ein Code an sie, falls keine Authenticator-App eingerichtet ist.'} +

+