@@ -23,6 +23,10 @@ import ConfirmPhone from './ConfirmPhone'
2323import ConfirmEmail from './ConfirmEmail'
2424import PasswordRule from './PasswordRule'
2525import 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
2731const 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} )
242250export 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 >
0 commit comments