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.
+
+ run(() => api.post('/auth/email/resend', {}), `Neuer Link an ${user.pending_email} gesendet.`)}
+ className="font-medium hover:underline disabled:opacity-60"
+ >
+ Link erneut senden
+
+ run(() => api.post('/auth/email/cancel', {}))}
+ className="font-medium hover:underline disabled:opacity-60"
+ >
+ Änderung verwerfen
+
+
+
+ )}
+ {notice &&
{notice}
}
+ {error &&
{error}
}
+
+ { setPhase('edit'); setEmail(''); setError(''); setNotice(''); }}
+ className="rounded-md bg-brand-500 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-600"
+ >
+ {user?.email ? 'E-Mail-Adresse ändern' : 'E-Mail-Adresse hinterlegen'}
+
+ {(user?.email || user?.pending_email) && (
+ { setPhase('remove'); setError(''); setNotice(''); }}
+ className="rounded-md border border-red-300 px-4 py-2 text-sm font-medium text-red-600 hover:bg-red-50 dark:border-red-900 dark:hover:bg-red-900/20"
+ >
+ Entfernen
+
+ )}
+
+ >
+ )}
+
+ {phase === 'edit' && (
+
+ )}
+
+ {phase === 'remove' && (
+
+ )}
+
+ );
+}
+
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"
/>
+
+
E-Mail-Adresse
+
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.'}
+
+
Passwort {editingId ? '(leer lassen = unverändert)' : '*'}
diff --git a/hifi-src/src/pages/admin/ConfirmEmail.jsx b/hifi-src/src/pages/admin/ConfirmEmail.jsx
new file mode 100644
index 0000000..225d592
--- /dev/null
+++ b/hifi-src/src/pages/admin/ConfirmEmail.jsx
@@ -0,0 +1,64 @@
+import { useEffect, useRef, useState } from 'react';
+import { Link, useSearchParams } from 'react-router-dom';
+import { api } from '../../api/client.js';
+import { useAuth } from '../../context/AuthContext.jsx';
+import logo from '../../assets/logo.png';
+import logoDark from '../../assets/logo-dark.png';
+
+// Ziel des Links aus der Bestaetigungsmail (AccountEmail::sendVerificationLink). Funktioniert
+// auch ohne Anmeldung, der Token im Link reicht.
+export default function ConfirmEmail() {
+ const [params] = useSearchParams();
+ const { user, refreshUser } = useAuth();
+ const [state, setState] = useState('loading'); // loading | ok | error
+ const [message, setMessage] = useState('');
+ const sent = useRef(false);
+
+ useEffect(() => {
+ // StrictMode ruft Effekte doppelt auf - der Token ist nach dem ersten Aufruf verbraucht.
+ if (sent.current) return;
+ sent.current = true;
+ api
+ .post('/auth/email/confirm', { token: params.get('token') || '' })
+ .then(() => {
+ setState('ok');
+ refreshUser().catch(() => {});
+ })
+ .catch((err) => {
+ setMessage(err.message);
+ setState('error');
+ });
+ }, []);
+
+ return (
+
+
+
+
+ {state === 'loading' &&
E-Mail-Adresse wird bestätigt…
}
+ {state === 'ok' && (
+ <>
+
E-Mail-Adresse bestätigt
+
+ Ab jetzt bekommst du beim Login einen Code an diese Adresse, sofern keine Authenticator-App eingerichtet ist.
+
+ >
+ )}
+ {state === 'error' && (
+ <>
+
Bestätigung fehlgeschlagen
+
{message}
+ >
+ )}
+ {state !== 'loading' && (
+
+ {user ? 'Zu meinem Konto' : 'Zum Login'}
+
+ )}
+
+
+ );
+}
diff --git a/hifi-src/src/pages/admin/Login.jsx b/hifi-src/src/pages/admin/Login.jsx
index 238d12c..07bb2dc 100644
--- a/hifi-src/src/pages/admin/Login.jsx
+++ b/hifi-src/src/pages/admin/Login.jsx
@@ -5,9 +5,12 @@ import logo from '../../assets/logo.png';
import logoDark from '../../assets/logo-dark.png';
export default function Login() {
- const { user, login, verifyTwoFactor } = useAuth();
+ const { user, login, verifyTwoFactor, resendLoginCode } = useAuth();
const navigate = useNavigate();
const [step, setStep] = useState('credentials');
+ const [method, setMethod] = useState('totp');
+ const [emailHint, setEmailHint] = useState('');
+ const [notice, setNotice] = useState('');
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [code, setCode] = useState('');
@@ -25,6 +28,9 @@ export default function Login() {
try {
const result = await login(username, password);
if (result.requires2fa) {
+ setMethod(result.method);
+ setEmailHint(result.emailHint);
+ setNotice('');
setStep('code');
} else {
navigate('/admin');
@@ -50,6 +56,17 @@ export default function Login() {
}
};
+ const handleResend = async () => {
+ setError('');
+ setNotice('');
+ try {
+ await resendLoginCode();
+ setNotice('Neuer Code ist unterwegs.');
+ } catch (err) {
+ setError(err.message);
+ }
+ };
+
return (