Fix double email_user/confirm due to language render (patch)

Patches previous attempt at f0ef9261

- The flag needs to be set immediately, not in the `then` block.
- Renamed the flag for clarity.
This commit is contained in:
infinite-persistence 2022-04-05 22:36:18 +08:00 committed by Thomas Zarebczan
parent 4f4803c638
commit 13f9f24af7

View file

@ -14,14 +14,16 @@ type Props = {
doToast: ({}) => void, doToast: ({}) => void,
}; };
let authenticationCompleted = false; // Guard against parents remounting. We don't want to send multi api calls.
let verificationApiHistory = { called: false, successful: false };
function SignInVerifyPage(props: Props) { function SignInVerifyPage(props: Props) {
const { const {
history: { push, location }, history: { push, location },
doToast, doToast,
} = props; } = props;
const [isAuthenticationSuccess, setIsAuthenticationSuccess] = useState(authenticationCompleted);
const [isAuthenticationSuccess, setIsAuthenticationSuccess] = useState(verificationApiHistory.successful);
const [showCaptchaMessage, setShowCaptchaMessage] = useState(false); const [showCaptchaMessage, setShowCaptchaMessage] = useState(false);
const [captchaLoaded, setCaptchaLoaded] = useState(false); const [captchaLoaded, setCaptchaLoaded] = useState(false);
const urlParams = new URLSearchParams(location.search); const urlParams = new URLSearchParams(location.search);
@ -30,6 +32,8 @@ function SignInVerifyPage(props: Props) {
const verificationToken = urlParams.get('verification_token'); const verificationToken = urlParams.get('verification_token');
const needsRecaptcha = urlParams.get('needs_recaptcha') === 'true'; const needsRecaptcha = urlParams.get('needs_recaptcha') === 'true';
const successful = isAuthenticationSuccess || verificationApiHistory.successful;
function onAuthError(message) { function onAuthError(message) {
doToast({ doToast({
message: message || __('Authentication failure.'), message: message || __('Authentication failure.'),
@ -45,9 +49,10 @@ function SignInVerifyPage(props: Props) {
}, [authToken, userSubmittedEmail, verificationToken, doToast, push]); }, [authToken, userSubmittedEmail, verificationToken, doToast, push]);
React.useEffect(() => { React.useEffect(() => {
if (!needsRecaptcha && !isAuthenticationSuccess) { if (!needsRecaptcha && !isAuthenticationSuccess && !verificationApiHistory.called) {
verifyUser(); verifyUser();
} }
// eslint-disable-next-line react-hooks/exhaustive-deps, (on mount only)
}, []); }, []);
React.useEffect(() => { React.useEffect(() => {
@ -75,6 +80,7 @@ function SignInVerifyPage(props: Props) {
} }
function verifyUser(captchaValue) { function verifyUser(captchaValue) {
verificationApiHistory.called = true;
Lbryio.call('user_email', 'confirm', { Lbryio.call('user_email', 'confirm', {
auth_token: authToken, auth_token: authToken,
email: userSubmittedEmail, email: userSubmittedEmail,
@ -82,10 +88,11 @@ function SignInVerifyPage(props: Props) {
...(captchaValue ? { recaptcha: captchaValue } : {}), ...(captchaValue ? { recaptcha: captchaValue } : {}),
}) })
.then(() => { .then(() => {
authenticationCompleted = true; verificationApiHistory.successful = true;
setIsAuthenticationSuccess(true); setIsAuthenticationSuccess(true);
}) })
.catch(() => { .catch(() => {
verificationApiHistory.successful = false;
onAuthError(__('Invalid captcha response or other authentication error.')); onAuthError(__('Invalid captcha response or other authentication error.'));
}); });
} }
@ -94,17 +101,17 @@ function SignInVerifyPage(props: Props) {
<Page authPage noFooter> <Page authPage noFooter>
<div className="main__sign-up"> <div className="main__sign-up">
<Card <Card
title={isAuthenticationSuccess ? __('Log in success!') : __('Log in')} title={successful ? __('Log in success!') : __('Log in')}
subtitle={ subtitle={
<React.Fragment> <React.Fragment>
<p> <p>
{isAuthenticationSuccess {successful
? __('You can now close this tab.') ? __('You can now close this tab.')
: needsRecaptcha : needsRecaptcha
? null ? null
: __('Welcome back! You are automatically being signed in.')} : __('Welcome back! You are automatically being signed in.')}
</p> </p>
{showCaptchaMessage && !isAuthenticationSuccess && ( {showCaptchaMessage && !successful && (
<p> <p>
<I18nMessage <I18nMessage
tokens={{ tokens={{
@ -120,7 +127,7 @@ function SignInVerifyPage(props: Props) {
</React.Fragment> </React.Fragment>
} }
actions={ actions={
!isAuthenticationSuccess && !successful &&
needsRecaptcha && ( needsRecaptcha && (
<div className="section__actions"> <div className="section__actions">
<ReCAPTCHA <ReCAPTCHA