Skip to content

Commit 3e5e20e

Browse files
committed
Adicionar recaptcha ao login
1 parent 8c4bfb4 commit 3e5e20e

5 files changed

Lines changed: 84 additions & 22 deletions

File tree

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@
7979
"moment": "2.24.0",
8080
"platform": "1.3.5",
8181
"react-custom-scrollbars": "4.2.1",
82+
"react-google-recaptcha-v3": "1.9.4",
8283
"react-helmet-async": "1.0.3",
8384
"react-i18nify": "1.11.18",
85+
"react-recaptcha-v3": "2.0.1",
8486
"react-text-mask": "5.4.3",
8587
"text-mask-addons": "3.8.0"
8688
}

src/app/App/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class App extends Foundation {
3737
ssoError: false
3838
}
3939
componentDidMount() {
40+
4041
if (this.props.multitenant) {
4142
RealmStore.fetch().then(realms => {
4243
const hostname = window.location.hostname
@@ -84,6 +85,7 @@ class App extends Foundation {
8485
RealmStore.menuBackground = this.props.menuBackground
8586
AppStore.menuFixed = !!this.props.menuFixed
8687
AppStore.startExpanded = !!this.props.startExpanded
88+
8789
}
8890

8991
resolveCurrent = () => {

src/app/Login/index.js

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import ConfirmPhone from './ConfirmPhone'
2323
import ConfirmEmail from './ConfirmEmail'
2424
import PasswordRule from './PasswordRule'
2525
import cs from 'classnames'
26+
// import { loadReCaptcha } from 'react-recaptcha-v3'
27+
import { ReCaptcha } from 'react-recaptcha-v3'
28+
import { loadReCaptcha } from 'react-recaptcha-v3'
29+
2630

2731
const styles = theme => ({
2832
root: {
@@ -237,6 +241,10 @@ const styles = theme => ({
237241
},
238242
icon: {
239243
height: 90
244+
},
245+
centered: {
246+
margin: 'auto',
247+
marginTop: '20px',
240248
}
241249
})
242250
export default
@@ -275,10 +283,23 @@ class Login extends React.Component {
275283
newUser: false,
276284
allowSendEmail: true,
277285
userInconsistent: false,
278-
noTestify: false
286+
noTestify: false,
287+
recaptchaSate: ''
288+
}
289+
290+
componentDidMount() {
291+
loadReCaptcha(process.env.REACT_APP_RECAPTCHA_SITE_KEY)
279292
}
280293

281-
componentDidMount() { }
294+
verifyCallback = (recaptchaToken) => {
295+
if(recaptchaToken){
296+
this.setState({recaptchaSate: recaptchaToken})
297+
}
298+
}
299+
300+
updateToken = () => {
301+
this.recaptcha.execute();
302+
}
282303

283304
login = e => {
284305
UserStore.error = null
@@ -314,16 +335,20 @@ class Login extends React.Component {
314335
}
315336

316337
check = e => {
317-
UserStore.error = null
318-
UserStore.check(this.props.loginType === 'cpf' ? this.state.username.replace(/\./g, '').replace(/-/g, '') : this.state.username).then(result => {
319-
if (result) {
338+
UserStore.error = null
339+
UserStore.check(this.props.loginType === 'cpf' ? this.state.username.replace(/\./g, '').replace(/-/g, '') : this.state.username, this.state.recaptchaSate).then(result => {
340+
if (result) {
320341
AppStore.setEmail(result.email)
321-
if (result.code === 1) {
342+
if (result.code === 6) {
343+
// Invalid recaptcha response
344+
this.setState({error: 'Por favor, tente novamente.'}, this.updateToken())
345+
} else if (result.code === 1) {
322346
// Common valid user
323347
this.go('PasswordPage')
324348
} else if (result.code === 2 || result.code === 3) {
325349
// User invalid
326350
if (AppStore.email || RealmStore.confirmationMethod === 'CPF' || RealmStore.confirmationMethod === 'DISABLED') {
351+
console.log(result)
327352
this.go('FirstLoginPage')
328353
this.setState({ requireValidationKey: result.code === 3 }, () => this.go('FirstLoginPage'))
329354
} else {
@@ -428,8 +453,7 @@ class Login extends React.Component {
428453
}
429454
)
430455
}
431-
432-
resolveSubmit = e => {
456+
resolveSubmit = e => {
433457
switch (this.state.page) {
434458
default:
435459
return this.handleSubmitLoginPage
@@ -454,10 +478,11 @@ class Login extends React.Component {
454478
}
455479
}
456480

457-
handleSubmitLoginPage = e => {
458-
e.preventDefault()
459-
if (this.state.usernameValid) {
460-
this.check()
481+
handleSubmitLoginPage = e => {
482+
e.preventDefault()
483+
if (this.state.usernameValid) {
484+
this.setState({ error: false }, this.check())
485+
461486
} else {
462487
this.setState({ error: true })
463488
}
@@ -570,7 +595,8 @@ class Login extends React.Component {
570595
privacy,
571596
userInconsistent,
572597
newUser,
573-
noTestify
598+
noTestify,
599+
recaptchaSate
574600
} = this.state
575601
const { full, bottom } = RealmStore.logos || {}
576602

@@ -594,13 +620,19 @@ class Login extends React.Component {
594620
return <PageLoading />
595621
}
596622

597-
let mainTenant = UserStore.realm === 'incentiveme'
623+
let mainTenant = UserStore.realm === 'incentiveme'
598624

599625
return (
600-
<Background>
626+
<Background>
601627
<div className={classes.root}>
602628
<Card className={classes.card}>
603-
<form className={classes.flex} onSubmit={this.resolveSubmit()} noValidate>
629+
<form className={classes.flex} onSubmit={this.resolveSubmit()} noValidate>
630+
<ReCaptcha
631+
ref={ref => this.recaptcha = ref}
632+
sitekey={process.env.REACT_APP_RECAPTCHA_SITE_KEY}
633+
action='submit'
634+
verifyCallback={this.verifyCallback}
635+
/>
604636
<LinearLayout visible={!UserStore.busy() || !!UserStore.error} flex={1}>
605637
{full ? (
606638
<div className={cs(classes.logoContainer, AppStore.device.hasNotch ? classes.logoContainerIos : classes.logoContainerDefault)}>
@@ -611,6 +643,7 @@ class Login extends React.Component {
611643
)}
612644
{!UserStore.logged || UserStore.logged.enabled ? (
613645
<div className={classes.formWrapper}>
646+
614647
{(() => {
615648
switch (page) {
616649
default:
@@ -767,9 +800,14 @@ class Login extends React.Component {
767800
</React.Fragment>
768801
)}
769802
{page !== 'ConfirmEmailPage' && page !== 'ConfirmPhonePage' && page !== 'AdminLogin' ? (
770-
<Button id="sign-in-button" className={classes.button} type="submit" color="secondary" variant="contained" fullWidth>
771-
Continuar
772-
</Button>
803+
recaptchaSate === '' ?
804+
<div className={classes.centered}>
805+
<CircularProgress />
806+
</div>
807+
:
808+
<Button id="sign-in-button" className={classes.button} type="submit" color="secondary" variant="contained" fullWidth>
809+
Continuar
810+
</Button>
773811
) : null}
774812

775813
{/*page === 'LoginPage' && (
@@ -834,7 +872,7 @@ class Login extends React.Component {
834872
) : (
835873
<span>{AppStore.messages.userBlockedWithoutEmail}</span>
836874
)}
837-
</div>
875+
</div>
838876
<Button className={classes.button} onClick={this.logout} color="secondary" variant="contained" fullWidth>
839877
Continuar
840878
</Button>

src/stores/UserStore.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,11 @@ class Users extends Collection {
127127
email,
128128
})
129129

130-
check = username =>
130+
check = (username, recaptchaToken) =>
131131
this.rpc('check', {
132132
realm: this.realm,
133133
username,
134+
recaptchaToken
134135
})
135136

136137
sendVerification = () => this.rpc('send-verification')

yarn.lock

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3591,6 +3591,13 @@ highlight.js@^9.15.5:
35913591
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-9.15.6.tgz#72d4d8d779ec066af9a17cb14360c3def0aa57c4"
35923592
integrity sha512-zozTAWM1D6sozHo8kqhfYgsac+B+q0PmsjXeyDrYIHHcBN0zTVT66+s2GW1GZv7DbyaROdLXKdabwS/WqPyIdQ==
35933593

3594+
hoist-non-react-statics@3.3.2:
3595+
version "3.3.2"
3596+
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz#ece0acaf71d62c2969c2ec59feff42a4b1a85b45"
3597+
integrity sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==
3598+
dependencies:
3599+
react-is "^16.7.0"
3600+
35943601
hosted-git-info@^2.1.4:
35953602
version "2.7.1"
35963603
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.7.1.tgz#97f236977bd6e125408930ff6de3eec6281ec047"
@@ -6194,6 +6201,13 @@ react-fast-compare@2.0.4:
61946201
resolved "https://registry.yarnpkg.com/react-fast-compare/-/react-fast-compare-2.0.4.tgz#e84b4d455b0fec113e0402c329352715196f81f9"
61956202
integrity sha512-suNP+J1VU1MWFKcyt7RtjiSWUjvidmQSlqu+eHslq+342xCbGTYmC0mEhPCOHxlW0CywylOC1u2DFAT+bv4dBw==
61966203

6204+
react-google-recaptcha-v3@1.9.4:
6205+
version "1.9.4"
6206+
resolved "https://registry.yarnpkg.com/react-google-recaptcha-v3/-/react-google-recaptcha-v3-1.9.4.tgz#9121d8cda048ae6f6014d198c3f7f39d3295ea90"
6207+
integrity sha512-WfDzE6WxhM6N2hEGUvPsRPvnxE/fLLEER4g8GAdqExW+MI7MCEjDIpsVkQtNvYo6H2/XRvUjcHStEp9sk0trVg==
6208+
dependencies:
6209+
hoist-non-react-statics "3.3.2"
6210+
61976211
react-helmet-async@1.0.3:
61986212
version "1.0.3"
61996213
resolved "https://registry.yarnpkg.com/react-helmet-async/-/react-helmet-async-1.0.3.tgz#68a176dd266c2caf63762879c573a866b89a2098"
@@ -6214,7 +6228,7 @@ react-i18nify@1.11.18:
62146228
moment "^2.22.1"
62156229
prop-types "^15.6.1"
62166230

6217-
react-is@^16.8.0:
6231+
react-is@^16.7.0, react-is@^16.8.0:
62186232
version "16.13.1"
62196233
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
62206234
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
@@ -6224,6 +6238,11 @@ react-is@^16.8.1, react-is@^16.8.4:
62246238
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.8.6.tgz#5bbc1e2d29141c9fbdfed456343fe2bc430a6a16"
62256239
integrity sha512-aUk3bHfZ2bRSVFFbbeVS4i+lNPZr3/WM5jT2J5omUVV1zzcs1nAaf3l51ctA5FFvCRbhrH0bdAsRRQddFJZPtA==
62266240

6241+
react-recaptcha-v3@2.0.1:
6242+
version "2.0.1"
6243+
resolved "https://registry.yarnpkg.com/react-recaptcha-v3/-/react-recaptcha-v3-2.0.1.tgz#b8dd391e9e36785f250d5e01aad235dd2fea2285"
6244+
integrity sha512-ZQ+auotgu+E/6YAbPqk53sf9xgcp8TFvVVfL4bVR7PXj98nQmdaONugdLJEA7g1iTZ4yQqC4mZbcVUGZmwb25Q==
6245+
62276246
react-text-mask@5.4.3:
62286247
version "5.4.3"
62296248
resolved "https://registry.yarnpkg.com/react-text-mask/-/react-text-mask-5.4.3.tgz#991efb4299e30c2e6c2c46d13f617169463e0d2d"

0 commit comments

Comments
 (0)