import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useAuth } from '../context/AuthContext';
import { authApi } from '../services/api';
import Button from '../components/ui/Button';
import Input from '../components/ui/Input';
import Card from '../components/ui/Card';
const MIN_LENGTH = 12;
function checkComplexity(pw: string) {
return {
length: pw.length >= MIN_LENGTH,
upper: /[A-Z]/.test(pw),
lower: /[a-z]/.test(pw),
digit: /[0-9]/.test(pw),
special: /[^A-Za-z0-9]/.test(pw),
};
}
function ComplexityHint({ pw }: { pw: string }) {
const c = checkComplexity(pw);
const items: [boolean, string][] = [
[c.length, `Mindestens ${MIN_LENGTH} Zeichen`],
[c.upper, 'Großbuchstabe'],
[c.lower, 'Kleinbuchstabe'],
[c.digit, 'Ziffer'],
[c.special, 'Sonderzeichen'],
];
return (
{items.map(([ok, label]) => (
-
{ok ? '✓' : '○'} {label}
))}
);
}
export default function ChangeInitialPassword() {
const { logout, user } = useAuth();
const navigate = useNavigate();
const [newPassword, setNewPassword] = useState('');
const [repeat, setRepeat] = useState('');
const [error, setError] = useState('');
const [isSaving, setIsSaving] = useState(false);
const c = checkComplexity(newPassword);
const meetsComplexity = c.length && c.upper && c.lower && c.digit && c.special;
const matches = newPassword.length > 0 && newPassword === repeat;
const canSubmit = meetsComplexity && matches && !isSaving;
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError('');
if (!canSubmit) return;
setIsSaving(true);
try {
const res = await authApi.changeInitialPortalPassword(newPassword);
if (!res.success) {
throw new Error(res.error || 'Passwort konnte nicht geändert werden');
}
await logout();
navigate('/login?changed=1', { replace: true });
} catch (err) {
setError(err instanceof Error ? err.message : 'Fehler beim Setzen des Passworts');
setIsSaving(false);
}
};
return (
Neues Passwort vergeben
Hallo {user?.firstName || 'Kunde'}, Sie haben sich mit einem Einmalpasswort
angemeldet. Bitte vergeben Sie jetzt Ihr eigenes Passwort. Danach werden Sie
ausgeloggt und können sich mit dem neuen Passwort anmelden.
{error && (
{error}
)}
);
}