import { useState, type FC } from 'react'; import { useNavigate, Link } from 'react-router-dom'; import { Container, Paper, TextField, Button, Typography, Box, Alert, CircularProgress, } from '@mui/material'; import { Lock as LockIcon } from '@mui/icons-material'; import { useAuthStore } from '../store/useStore'; import { unlockWithPassword, deriveAuthAndEncKeys, setEncKey } from '../crypto'; export const UnlockPage: FC = () => { const navigate = useNavigate(); const { wrapped_dek, wrapped_dek_iv, user } = useAuthStore(); const [password, setPassword] = useState(''); const [error, setError] = useState(''); const [unlocking, setUnlocking] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); setUnlocking(true); try { if (wrapped_dek && wrapped_dek_iv) { // Wrapped-DEK model: unwrap the DEK using the password. const { dek } = await unlockWithPassword(password, { data: wrapped_dek, iv: wrapped_dek_iv, }); setEncKey(dek); } else { // Phase 1 compat: derive the key directly from the password. const { encKey } = await deriveAuthAndEncKeys(password); setEncKey(encKey); } navigate('/', { replace: true }); } catch { setError('Incorrect password. Please try again.'); } finally { setUnlocking(false); } }; return ( Unlock {user?.username ? `Welcome back, ${user.username}. Enter your password to decrypt your data.` : 'Enter your password to decrypt your data.'} {error && ( {error} )} setPassword(e.target.value)} disabled={unlocking} /> Sign in with a different account Forgot password? ); }; export default UnlockPage;