import { useState } from 'react'; import QRCode from 'qrcode'; import { api } from '../../api/client.js'; import { useAuth } from '../../context/AuthContext.jsx'; const emptyPasswordForm = { current_password: '', new_password: '', new_password_confirmation: '', two_factor_code: '', }; export default function AccountSettings() { const { user, refreshUser } = useAuth(); const [phase, setPhase] = useState('idle'); // idle | setup | recovery-codes | disable | regenerate const [qrDataUrl, setQrDataUrl] = useState(''); const [secret, setSecret] = useState(''); const [code, setCode] = useState(''); const [password, setPassword] = useState(''); const [recoveryCodes, setRecoveryCodes] = useState([]); const [error, setError] = useState(''); const [busy, setBusy] = useState(false); const [passwordForm, setPasswordForm] = useState(emptyPasswordForm); const [passwordError, setPasswordError] = useState(''); const [passwordSuccess, setPasswordSuccess] = useState(''); const [passwordBusy, setPasswordBusy] = useState(false); const handleChangePassword = async (e) => { e.preventDefault(); setPasswordError(''); setPasswordSuccess(''); if (passwordForm.new_password !== passwordForm.new_password_confirmation) { setPasswordError('Die neuen Passwörter stimmen nicht überein'); return; } setPasswordBusy(true); try { await api.post('/auth/change-password', passwordForm); setPasswordForm(emptyPasswordForm); setPasswordSuccess('Passwort erfolgreich geändert.'); } catch (err) { setPasswordError(err.message); } finally { setPasswordBusy(false); } }; const startSetup = async () => { setError(''); setBusy(true); try { const data = await api.get('/auth/2fa/setup'); setSecret(data.secret); setQrDataUrl(await QRCode.toDataURL(data.otpauth_url)); setCode(''); setPhase('setup'); } catch (err) { setError(err.message); } finally { setBusy(false); } }; const confirmSetup = async (e) => { e.preventDefault(); setError(''); setBusy(true); try { const data = await api.post('/auth/2fa/enable', { code }); setRecoveryCodes(data.recovery_codes); setPhase('recovery-codes'); await refreshUser(); } catch (err) { setError(err.message); } finally { setBusy(false); } }; const finishRecoveryCodes = () => { setPhase('idle'); setRecoveryCodes([]); setPassword(''); }; const handleDisable = async (e) => { e.preventDefault(); setError(''); setBusy(true); try { await api.post('/auth/2fa/disable', { password }); setPassword(''); setPhase('idle'); await refreshUser(); } catch (err) { setError(err.message); } finally { setBusy(false); } }; const handleRegenerate = async (e) => { e.preventDefault(); setError(''); setBusy(true); try { const data = await api.post('/auth/2fa/recovery-codes', { password }); setRecoveryCodes(data.recovery_codes); setPhase('recovery-codes'); } catch (err) { setError(err.message); } finally { setBusy(false); } }; return (

Mein Konto

Angemeldet als {user?.username}{user?.is_super_admin ? ' (Super-Admin)' : ''}.

Passwort ändern

Vergib ein neues Passwort für dein Konto. {user?.two_factor_enabled && ' Da 2FA aktiv ist, musst du die Änderung zusätzlich mit deinem Code bestätigen.'}

setPasswordForm({ ...passwordForm, current_password: e.target.value })} required 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" />
setPasswordForm({ ...passwordForm, new_password: e.target.value })} required minLength={8} 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" />
setPasswordForm({ ...passwordForm, new_password_confirmation: e.target.value })} required minLength={8} 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" />
{user?.two_factor_enabled && (
setPasswordForm({ ...passwordForm, two_factor_code: e.target.value })} required inputMode="numeric" placeholder="123456 oder Wiederherstellungscode" 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" />
)} {passwordError &&

{passwordError}

} {passwordSuccess &&

{passwordSuccess}

}

Zwei-Faktor-Authentifizierung

Schützt dein Konto zusätzlich mit einem Code aus der Google Authenticator App.

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

Status:{' '} {user?.two_factor_enabled ? ( Aktiv ) : ( Nicht aktiv )}

{user?.two_factor_enabled ? (
) : ( )} )} {phase === 'setup' && (
  1. Öffne die Google Authenticator App (oder eine kompatible App).
  2. Scanne den folgenden QR-Code:
{qrDataUrl && QR-Code für Zwei-Faktor-Authentifizierung}

Geht das Scannen nicht? Gib diesen Code manuell ein:
{secret}

setCode(e.target.value)} required autoFocus inputMode="numeric" placeholder="123456" className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-center text-lg tracking-widest dark:border-neutral-700 dark:bg-neutral-900" />
{error &&

{error}

}
)} {phase === 'recovery-codes' && (

Wiederherstellungscodes – jetzt sicher speichern!

Jeder Code funktioniert einmalig, falls du keinen Zugriff mehr auf die Authenticator-App hast. Diese Codes werden nur jetzt angezeigt.

{recoveryCodes.map((c) => {c})}
)} {phase === 'disable' && (
setPassword(e.target.value)} required autoFocus 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" />
{error &&

{error}

}
)} {phase === 'regenerate' && (

Die bisherigen Wiederherstellungscodes werden ungültig.

setPassword(e.target.value)} required autoFocus 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" />
{error &&

{error}

}
)}
); }