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 (
Angemeldet als {user?.username}{user?.is_super_admin ? ' (Super-Admin)' : ''}.
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.'}
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 ? (Wiederherstellungscodes – jetzt sicher speichern!
Jeder Code funktioniert einmalig, falls du keinen Zugriff mehr auf die Authenticator-App hast. Diese Codes werden nur jetzt angezeigt.