File size: 4,872 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
import page from '@automattic/calypso-router';
import { FormInputValidation, FormLabel } from '@automattic/components';
import { Button, Spinner } from '@wordpress/components';
import { useTranslate } from 'i18n-calypso';
import { useState, useRef, useEffect } from 'react';
import FormTextInput from 'calypso/components/forms/form-text-input';
import { login } from 'calypso/lib/paths';
import { useDispatch } from 'calypso/state';
import { sendEmailLogin } from 'calypso/state/auth/actions';
const LostPasswordForm = ( {
redirectToAfterLoginUrl,
oauth2ClientId,
locale,
from,
isWooJPC,
isWoo,
isJetpack,
} ) => {
const translate = useTranslate();
const [ userLogin, setUserLogin ] = useState( '' );
const [ error, setError ] = useState( null );
const [ isBusy, setBusy ] = useState( false );
const dispatch = useDispatch();
const inputRef = useRef( null );
useEffect( () => {
inputRef.current?.focus();
}, [] );
const validateUserLogin = () => {
// Allow empty input or any non-empty value (username or email)
if ( userLogin.length === 0 ) {
setError( null );
} else if ( userLogin.includes( '@' ) ) {
// If it contains @, validate as email
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if ( emailRegex.test( userLogin ) ) {
setError( null );
} else {
setError( translate( 'Please enter a valid email address.' ) );
}
} else {
// Username - accept any non-empty value
setError( null );
}
};
const getAuthAccountTypeRequest = async ( userNameOrEmail ) => {
const resp = await window.fetch(
`https://public-api.wordpress.com/rest/v1.1/users/${ userNameOrEmail }/auth-options`,
{
method: 'GET',
}
);
if ( resp.status < 200 || resp.status >= 300 ) {
throw resp;
}
return await resp.json();
};
const lostPasswordRequest = async () => {
const formData = new FormData();
formData.set( 'user_login', userLogin );
const origin = typeof window !== 'undefined' ? window.location.origin : '';
const resp = await window.fetch( `${ origin }/wp-login.php?action=lostpassword`, {
method: 'POST',
body: formData,
credentials: 'include',
} );
if ( resp.status < 200 || resp.status >= 300 ) {
throw resp;
}
return await resp.text();
};
const onSubmit = async ( event ) => {
event.preventDefault();
if ( isWooJPC ) {
const accountType = await getAuthAccountTypeRequest( userLogin );
if ( accountType?.passwordless === true ) {
await dispatch(
sendEmailLogin( userLogin, {
redirectTo: redirectToAfterLoginUrl,
loginFormFlow: true,
showGlobalNotices: true,
flow: 'jetpack',
} )
);
page(
login( {
isJetpack: true,
// If no notification is sent, the user is using the authenticator for 2FA by default
twoFactorAuthType: 'link',
locale: locale,
from: from,
emailAddress: userLogin,
} )
);
return;
}
}
try {
setBusy( true );
const result = await lostPasswordRequest();
setBusy( false );
if ( result.includes( 'Unable to reset password' ) ) {
return setError(
translate( "I'm sorry, but we weren't able to find a user with that login information." )
);
}
page(
login( {
oauth2ClientId,
locale,
redirectTo: redirectToAfterLoginUrl,
emailAddress: userLogin,
lostpasswordFlow: true,
from,
isJetpack: isWooJPC || isJetpack,
} )
);
} catch ( _httpError ) {
setBusy( false );
setError(
translate( 'There was an error sending the password reset email. Please try again.' )
);
}
};
const showError = !! error;
return (
<form
name="lostpasswordform"
className="login__lostpassword-form"
method="post"
onSubmit={ onSubmit }
>
<div className="login__form-userdata">
<FormLabel htmlFor="userLogin">{ translate( 'Username or email address' ) }</FormLabel>
<FormTextInput
autoCapitalize="off"
autoCorrect="off"
spellCheck="false"
autoComplete="username"
id="userLogin"
name="userLogin"
type="text"
value={ userLogin }
isError={ showError }
onBlur={ validateUserLogin }
onChange={ ( event ) => {
const newValue = event.target.value.trim();
setUserLogin( newValue );
// Clear error immediately when user starts typing to fix input
if ( error ) {
setError( null );
}
} }
ref={ inputRef }
/>
{ showError && <FormInputValidation isError text={ error } /> }
</div>
<div className="login__form-action">
<Button
variant="primary"
type="submit"
disabled={ userLogin.length === 0 || showError || isBusy }
isBusy={ isBusy }
__next40pxDefaultSize
>
{ isBusy && isWoo ? <Spinner /> : translate( 'Reset my password' ) }
</Button>
</div>
</form>
);
};
export default LostPasswordForm;
|