lbry-desktop/ui/page/signInVerify/view.jsx

81 lines
2.4 KiB
React
Raw Normal View History

2019-11-14 01:33:36 +01:00
// @flow
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-14 01:33:36 +01:00
import Button from 'component/button';
import { Lbryio } from 'lbryinc';
import * as PAGES from 'constants/pages';
type Props = {
2019-11-14 15:34:55 +01:00
history: { push: string => void, location: { search: string } },
doToast: ({}) => void,
2019-11-14 01:33:36 +01:00
};
function SignInVerifyPage(props: Props) {
2019-11-14 15:34:55 +01:00
const {
history: { push, location },
doToast,
} = props;
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');
const [isAuthenticationSuccess, setIsAuthenticationSuccess] = useState(false);
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]);
function onCaptchaChange(value) {
Lbryio.call('user_email', 'confirm', {
auth_token: authToken,
email: userSubmittedEmail,
verification_token: verificationToken,
recaptcha: value,
})
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 (
<Page authPage className="main--auth-page">
<section className="main--contained">
2019-11-14 15:34:55 +01:00
<h1 className="section__title--large">
{isAuthenticationSuccess ? __('Sign In Success!') : __('Sign In to lbry.tv')}
</h1>
<p className="section__subtitle">
{isAuthenticationSuccess ? __('You can now close this tab.') : __('Click below to sign in to lbry.tv')}
</p>
{!isAuthenticationSuccess && (
<div className="section__actions">
<ReCAPTCHA
sitekey="6LePsJgUAAAAAFTuWOKRLnyoNKhm0HA4C3elrFMG"
onChange={onCaptchaChange}
onExpired={onAuthError}
onErrored={onAuthError}
/>
</div>
)}
2019-11-14 01:33:36 +01:00
</section>
</Page>
);
2019-11-14 15:34:55 +01:00
}
2019-11-14 01:33:36 +01:00
export default withRouter(SignInVerifyPage);