2019-11-14 01:33:36 +01:00
|
|
|
// @flow
|
2020-04-13 21:16:07 +02:00
|
|
|
import * as PAGES from 'constants/pages';
|
2019-11-14 01:33:36 +01:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { withRouter } from 'react-router';
|
|
|
|
import Page from 'component/page';
|
2019-11-14 15:34:55 +01:00
|
|
|
import ReCAPTCHA from 'react-google-recaptcha';
|
2019-11-19 18:22:08 +01:00
|
|
|
import Button from 'component/button';
|
2019-11-14 01:33:36 +01:00
|
|
|
import { Lbryio } from 'lbryinc';
|
2019-11-19 18:22:08 +01:00
|
|
|
import I18nMessage from 'component/i18nMessage';
|
2020-04-13 21:16:07 +02:00
|
|
|
import Card from 'component/common/card';
|
2021-07-14 05:45:51 +02:00
|
|
|
// @if TARGET='web'
|
2021-07-14 05:40:49 +02:00
|
|
|
import Pixel from 'web/component/pixel';
|
2021-07-14 05:45:51 +02:00
|
|
|
// @endif
|
2019-11-14 01:33:36 +01:00
|
|
|
type Props = {
|
2021-07-08 10:26:58 +02:00
|
|
|
history: { push: (string) => void, location: { search: string } },
|
2019-11-14 15:34:55 +01:00
|
|
|
doToast: ({}) => void,
|
2019-11-14 01:33:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
function SignInVerifyPage(props: Props) {
|
2019-11-14 15:34:55 +01:00
|
|
|
const {
|
2020-08-25 15:39:52 +02:00
|
|
|
history: { push, location },
|
2019-11-14 15:34:55 +01:00
|
|
|
doToast,
|
|
|
|
} = props;
|
2019-11-19 17:55:56 +01:00
|
|
|
const [isAuthenticationSuccess, setIsAuthenticationSuccess] = useState(false);
|
2019-11-19 18:22:08 +01:00
|
|
|
const [showCaptchaMessage, setShowCaptchaMessage] = useState(false);
|
|
|
|
const [captchaLoaded, setCaptchaLoaded] = useState(false);
|
2019-11-14 01:33:36 +01:00
|
|
|
const urlParams = new URLSearchParams(location.search);
|
|
|
|
const authToken = urlParams.get('auth_token');
|
|
|
|
const userSubmittedEmail = urlParams.get('email');
|
|
|
|
const verificationToken = urlParams.get('verification_token');
|
2019-11-19 17:55:56 +01:00
|
|
|
const needsRecaptcha = urlParams.get('needs_recaptcha') === 'true';
|
2019-11-14 01:33:36 +01:00
|
|
|
|
|
|
|
function onAuthError(message) {
|
|
|
|
doToast({
|
|
|
|
message: message || __('Authentication failure.'),
|
|
|
|
isError: true,
|
|
|
|
});
|
2019-11-14 15:30:26 +01:00
|
|
|
push(`/$/${PAGES.AUTH}`);
|
2019-11-14 01:33:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (!authToken || !userSubmittedEmail || !verificationToken) {
|
|
|
|
onAuthError(__('Invalid or expired sign-in link.'));
|
|
|
|
}
|
|
|
|
}, [authToken, userSubmittedEmail, verificationToken, doToast, push]);
|
|
|
|
|
2019-11-19 17:55:56 +01:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (!needsRecaptcha) {
|
|
|
|
verifyUser();
|
|
|
|
}
|
|
|
|
}, [needsRecaptcha]);
|
|
|
|
|
2019-11-19 18:22:08 +01:00
|
|
|
React.useEffect(() => {
|
|
|
|
let captchaTimeout;
|
|
|
|
|
|
|
|
if (needsRecaptcha && !captchaLoaded) {
|
|
|
|
captchaTimeout = setTimeout(() => {
|
|
|
|
setShowCaptchaMessage(true);
|
|
|
|
}, 2000);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (captchaTimeout) {
|
|
|
|
clearTimeout(captchaTimeout);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [needsRecaptcha, captchaLoaded]);
|
|
|
|
|
2019-11-14 01:33:36 +01:00
|
|
|
function onCaptchaChange(value) {
|
2019-11-19 17:55:56 +01:00
|
|
|
verifyUser(value);
|
|
|
|
}
|
|
|
|
|
2019-11-19 18:22:08 +01:00
|
|
|
function onCaptchaReady() {
|
|
|
|
setCaptchaLoaded(true);
|
|
|
|
}
|
|
|
|
|
2019-11-19 17:55:56 +01:00
|
|
|
function verifyUser(captchaValue) {
|
2019-11-14 01:33:36 +01:00
|
|
|
Lbryio.call('user_email', 'confirm', {
|
|
|
|
auth_token: authToken,
|
|
|
|
email: userSubmittedEmail,
|
|
|
|
verification_token: verificationToken,
|
2019-11-19 17:55:56 +01:00
|
|
|
...(captchaValue ? { recaptcha: captchaValue } : {}),
|
2019-11-14 01:33:36 +01:00
|
|
|
})
|
2019-11-14 15:34:55 +01:00
|
|
|
.then(() => {
|
|
|
|
setIsAuthenticationSuccess(true);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
onAuthError(__('Invalid captcha response or other authentication error.'));
|
|
|
|
});
|
2019-11-14 01:33:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-08-21 17:49:13 +02:00
|
|
|
<Page authPage>
|
2020-04-13 21:16:07 +02:00
|
|
|
<div className="main__sign-up">
|
|
|
|
<Card
|
2021-06-02 19:41:06 +02:00
|
|
|
title={isAuthenticationSuccess ? __('Log in success!') : __('Log in')}
|
2020-04-13 21:16:07 +02:00
|
|
|
subtitle={
|
|
|
|
<React.Fragment>
|
2020-08-25 15:39:52 +02:00
|
|
|
<p>
|
|
|
|
{isAuthenticationSuccess
|
|
|
|
? __('You can now close this tab.')
|
|
|
|
: needsRecaptcha
|
|
|
|
? null
|
|
|
|
: __('Welcome back! You are automatically being signed in.')}
|
|
|
|
</p>
|
2020-04-13 21:16:07 +02:00
|
|
|
{showCaptchaMessage && !isAuthenticationSuccess && (
|
|
|
|
<p>
|
|
|
|
<I18nMessage
|
|
|
|
tokens={{
|
|
|
|
refresh: (
|
|
|
|
<Button button="link" label={__('refreshing')} onClick={() => window.location.reload()} />
|
|
|
|
),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Not seeing a captcha? Check your ad blocker or try %refresh%.
|
|
|
|
</I18nMessage>
|
|
|
|
</p>
|
|
|
|
)}
|
|
|
|
</React.Fragment>
|
|
|
|
}
|
|
|
|
actions={
|
|
|
|
!isAuthenticationSuccess &&
|
|
|
|
needsRecaptcha && (
|
|
|
|
<div className="section__actions">
|
|
|
|
<ReCAPTCHA
|
|
|
|
sitekey="6LePsJgUAAAAAFTuWOKRLnyoNKhm0HA4C3elrFMG"
|
|
|
|
onChange={onCaptchaChange}
|
|
|
|
asyncScriptOnLoad={onCaptchaReady}
|
|
|
|
onExpired={onAuthError}
|
|
|
|
onErrored={onAuthError}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
2021-07-14 05:40:49 +02:00
|
|
|
{/* @if TARGET='web' */}
|
|
|
|
<Pixel type={'kill'} />
|
|
|
|
{/* @endif */}
|
2019-11-14 01:33:36 +01:00
|
|
|
</Page>
|
|
|
|
);
|
2019-11-14 15:34:55 +01:00
|
|
|
}
|
2019-11-14 01:33:36 +01:00
|
|
|
|
|
|
|
export default withRouter(SignInVerifyPage);
|