import { Button, Card, Dialog, FormInputValidation, FormLabel } from '@automattic/components';
import { supported } from '@github/webauthn-json';
import debugFactory from 'debug';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import CardHeading from 'calypso/components/card-heading';
import FormButton from 'calypso/components/forms/form-button';
import FormCheckbox from 'calypso/components/forms/form-checkbox';
import FormFieldset from 'calypso/components/forms/form-fieldset';
import FormVerificationCodeInput from 'calypso/components/forms/form-verification-code-input';
import Notice from 'calypso/components/notice';
import WarningCard from 'calypso/components/warning-card';
import { recordGoogleEvent } from 'calypso/state/analytics/actions';
import { redirectToLogout } from 'calypso/state/current-user/actions';
import SecurityKeyForm from './security-key-form';
import TwoFactorActions from './two-factor-actions';
import './style.scss';
const debug = debugFactory( 'calypso:me:reauth-required' );
// autofocus is used for tracking purposes, not an a11y issue
/* eslint-disable jsx-a11y/no-autofocus, jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions, jsx-a11y/anchor-is-valid */
class ReauthRequired extends Component {
codeRequestTimer = false;
constructor( props ) {
super( props );
this.state = {
remember2fa: false, // Should the 2fa be remembered for 30 days?
code: '', // User's generated 2fa code
smsRequestsAllowed: true, // Can the user request another SMS code?
twoFactorAuthType: 'authenticator',
};
}
componentDidMount() {
this.props.twoStepAuthorization.on( 'change', this.update );
}
componentWillUnmount() {
clearTimeout( this.codeRequestTimer );
this.props.twoStepAuthorization.off( 'change', this.update );
}
update = () => {
this.forceUpdate();
};
getClickHandler = ( action, callback ) => () => {
this.props.recordGoogleEvent( 'Me', 'Clicked on ' + action );
if ( callback ) {
callback();
}
};
getCheckboxHandler = ( checkboxName ) => ( event ) => {
const action = 'Clicked ' + checkboxName + ' checkbox';
const value = event.target.checked ? 1 : 0;
this.props.recordGoogleEvent( 'Me', action, 'checked', value );
};
getFocusHandler = ( action ) => () =>
this.props.recordGoogleEvent( 'Me', 'Focused on ' + action );
renderCodeMessage() {
if ( this.props.twoStepAuthorization.isTwoStepSMSEnabled() ) {
return this.props.translate(
'Press the button below to request an SMS verification code. ' +
'Once you receive our text message at your phone number ending with ' +
'{{strong}}%(smsLastFour)s{{/strong}} , enter the code below.',
{
args: {
smsLastFour: this.props.twoStepAuthorization.getSMSLastFour(),
},
components: {
strong: ,
},
}
);
}
if ( this.state.twoFactorAuthType === 'sms' ) {
return this.props.translate(
'We just sent you a verification code to your phone number on file, please enter the code below.'
);
}
return this.props.translate(
'Please enter the verification code generated by your authenticator app.'
);
}
submitForm = ( event ) => {
event.preventDefault();
this.setState( { validatingCode: true } );
this.props.twoStepAuthorization.validateCode(
{
code: this.state.code,
remember2fa: this.state.remember2fa,
},
( error, data ) => {
this.setState( { validatingCode: false } );
if ( error ) {
debug( 'There was an error validating that code: ' + JSON.stringify( error ) );
} else {
debug( 'The code validated!' + JSON.stringify( data ) );
}
}
);
};
sendSMSCode() {
this.setState( { smsRequestsAllowed: false } );
this.codeRequestTimer = setTimeout( () => {
this.setState( { smsRequestsAllowed: true } );
}, 60000 );
this.props.twoStepAuthorization.sendSMSCode( ( error, data ) => {
if ( ! error && data.sent ) {
debug( 'SMS code successfully sent' );
} else {
debug( 'There was a failure sending the SMS code.' );
}
} );
}
preValidateAuthCode() {
return this.state.code.length && this.state.code.length > 5;
}
renderFailedValidationMsg() {
if ( ! this.props.twoStepAuthorization.codeValidationFailed() ) {
return null;
}
return (
{ this.renderCodeMessage() }