// @flow import * as PAGES from 'constants/pages'; import { SITE_NAME } from 'config'; import React, { useState } from 'react'; import { withRouter } from 'react-router'; import Page from 'component/page'; import ReCAPTCHA from 'react-google-recaptcha'; import Button from 'component/button'; import { Lbryio } from 'lbryinc'; import I18nMessage from 'component/i18nMessage'; import Card from 'component/common/card'; type Props = { history: { push: string => void, replace: string => void, location: { search: string } }, doToast: ({}) => void, }; function SignInVerifyPage(props: Props) { const { history: { push, replace, location }, doToast, } = props; const [isAuthenticationSuccess, setIsAuthenticationSuccess] = useState(false); const [showCaptchaMessage, setShowCaptchaMessage] = useState(false); const [captchaLoaded, setCaptchaLoaded] = useState(false); const urlParams = new URLSearchParams(location.search); const authToken = urlParams.get('auth_token'); const userSubmittedEmail = urlParams.get('email'); const verificationToken = urlParams.get('verification_token'); const needsRecaptcha = urlParams.get('needs_recaptcha') === 'true'; function onAuthError(message) { doToast({ message: message || __('Authentication failure.'), isError: true, }); push(`/$/${PAGES.AUTH}`); } React.useEffect(() => { if (!authToken || !userSubmittedEmail || !verificationToken) { onAuthError(__('Invalid or expired sign-in link.')); } }, [authToken, userSubmittedEmail, verificationToken, doToast, push]); React.useEffect(() => { if (!needsRecaptcha) { verifyUser(); } }, [needsRecaptcha]); React.useEffect(() => { let captchaTimeout; if (needsRecaptcha && !captchaLoaded) { captchaTimeout = setTimeout(() => { setShowCaptchaMessage(true); }, 2000); } return () => { if (captchaTimeout) { clearTimeout(captchaTimeout); } }; }, [needsRecaptcha, captchaLoaded]); React.useEffect(() => { if (isAuthenticationSuccess) { doToast({ message: __('You are now signed in.') }); replace('/'); } }, [isAuthenticationSuccess, replace, doToast]); function onCaptchaChange(value) { verifyUser(value); } function onCaptchaReady() { setCaptchaLoaded(true); } function verifyUser(captchaValue) { Lbryio.call('user_email', 'confirm', { auth_token: authToken, email: userSubmittedEmail, verification_token: verificationToken, ...(captchaValue ? { recaptcha: captchaValue } : {}), }) .then(() => { setIsAuthenticationSuccess(true); }) .catch(() => { onAuthError(__('Invalid captcha response or other authentication error.')); }); } return (

{needsRecaptcha ? null : __('Welcome back! You are automatically being signed in.')}

{showCaptchaMessage && !isAuthenticationSuccess && (

window.location.reload()} /> ), }} > Not seeing a captcha? Check your ad blocker or try %refresh%.

)} } actions={ !isAuthenticationSuccess && needsRecaptcha && (
) } />
); } export default withRouter(SignInVerifyPage);