import { STRING_NON_MATCHING_PASSWORDS } from '@/Strings' import { WebApplication } from '@/UIModels/Application' import { AppState } from '@/UIModels/AppState' import { observer } from 'mobx-react-lite' import { FunctionComponent } from 'preact' import { useEffect, useRef, useState } from 'preact/hooks' import { AccountMenuPane } from '.' import { Button } from '@/Components/Button/Button' import { Checkbox } from '@/Components/Checkbox' import { DecoratedPasswordInput } from '@/Components/Input/DecoratedPasswordInput' import { Icon } from '@/Components/Icon' import { IconButton } from '@/Components/Button/IconButton' type Props = { appState: AppState application: WebApplication setMenuPane: (pane: AccountMenuPane) => void email: string password: string } export const ConfirmPassword: FunctionComponent = observer( ({ application, appState, setMenuPane, email, password }) => { const { notesAndTagsCount } = appState.accountMenu const [confirmPassword, setConfirmPassword] = useState('') const [isRegistering, setIsRegistering] = useState(false) const [isEphemeral, setIsEphemeral] = useState(false) const [shouldMergeLocal, setShouldMergeLocal] = useState(true) const [error, setError] = useState('') const passwordInputRef = useRef(null) useEffect(() => { passwordInputRef.current?.focus() }, []) const handlePasswordChange = (text: string) => { setConfirmPassword(text) } const handleEphemeralChange = () => { setIsEphemeral(!isEphemeral) } const handleShouldMergeChange = () => { setShouldMergeLocal(!shouldMergeLocal) } const handleKeyDown = (e: KeyboardEvent) => { if (error.length) { setError('') } if (e.key === 'Enter') { handleConfirmFormSubmit(e) } } const handleConfirmFormSubmit = (e: Event) => { e.preventDefault() if (!password) { passwordInputRef.current?.focus() return } if (password === confirmPassword) { setIsRegistering(true) application .register(email, password, isEphemeral, shouldMergeLocal) .then((res) => { if (res.error) { throw new Error(res.error.message) } appState.accountMenu.closeAccountMenu() appState.accountMenu.setCurrentPane(AccountMenuPane.GeneralMenu) }) .catch((err) => { console.error(err) setError(err.message) }) .finally(() => { setIsRegistering(false) }) } else { setError(STRING_NON_MATCHING_PASSWORDS) setConfirmPassword('') passwordInputRef.current?.focus() } } const handleGoBack = () => { setMenuPane(AccountMenuPane.Register) } return ( <>
Confirm password
Because your notes are encrypted using your password,{' '} Standard Notes does not have a password reset option. If you forget your password, you will permanently lose access to your data.
]} onChange={handlePasswordChange} onKeyDown={handleKeyDown} placeholder="Confirm password" ref={passwordInputRef} value={confirmPassword} /> {error ?
{error}
: null}