-
Notifications
You must be signed in to change notification settings - Fork 415
/
view.jsx
138 lines (126 loc) · 4.06 KB
/
view.jsx
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
// @flow
import * as PAGES from 'constants/pages';
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, location: { search: string } },
doToast: ({}) => void,
};
function SignInVerifyPage(props: Props) {
const {
history: { push, 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]);
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 (
<Page authPage noFooter>
<div className="main__sign-up">
<Card
title={isAuthenticationSuccess ? __('Log in success!') : __('Log in')}
subtitle={
<React.Fragment>
<p>
{isAuthenticationSuccess
? __('You can now close this tab.')
: needsRecaptcha
? null
: __('Welcome back! You are automatically being signed in.')}
</p>
{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>
</Page>
);
}
export default withRouter(SignInVerifyPage);