import { useState } from 'react'; import { Resolver, useForm } from 'react-hook-form'; import { useNavigate } from 'react-router-dom'; import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons'; import { useStronghold } from '@stores/stronghold'; import { useAccount } from '@utils/hooks/useAccount'; import { useSecureStorage } from '@utils/hooks/useSecureStorage'; type FormValues = { password: string; }; const resolver: Resolver = async (values) => { return { values: values.password ? values : {}, errors: !values.password ? { password: { type: 'required', message: 'This is required.', }, } : {}, }; }; export function UnlockScreen() { const navigate = useNavigate(); const setPrivkey = useStronghold((state) => state.setPrivkey); const [passwordInput, setPasswordInput] = useState('password'); const [loading, setLoading] = useState(false); const { account } = useAccount(); const { load } = useSecureStorage(); // toggle private key const showPassword = () => { if (passwordInput === 'password') { setPasswordInput('text'); } else { setPasswordInput('password'); } }; const { register, setError, handleSubmit, formState: { errors, isDirty, isValid }, } = useForm({ resolver }); const onSubmit = async (data: { [x: string]: string }) => { setLoading(true); if (data.password.length > 3) { // load private in secure storage try { const privkey = await load(account.pubkey, data.password); setPrivkey(privkey); // redirect to home navigate('/', { replace: true }); } catch { setLoading(false); setError('password', { type: 'custom', message: 'Wrong password', }); } } else { setLoading(false); setError('password', { type: 'custom', message: 'Password is required and must be greater than 3', }); } }; return (

Enter password to unlock

{errors.password &&

{errors.password.message}

}
); }