File size: 5,946 Bytes
1e92f2d |
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
import { Card, FormInputValidation, FormLabel } from '@automattic/components';
import { localizeUrl } from '@automattic/i18n-utils';
import { Button } from '@wordpress/components';
import { localize } from 'i18n-calypso';
import { defer } from 'lodash';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import ActionPanelLink from 'calypso/components/action-panel/link';
import FormFieldset from 'calypso/components/forms/form-fieldset';
import FormVerificationCodeInput from 'calypso/components/forms/form-verification-code-input';
import { recordTracksEventWithClientId as recordTracksEvent } from 'calypso/state/analytics/actions';
import {
formUpdate,
loginUserWithTwoFactorVerificationCode,
sendSmsCode,
} from 'calypso/state/login/actions';
import { getTwoFactorAuthRequestError } from 'calypso/state/login/selectors';
import TwoFactorActions from './two-factor-actions';
import './verification-code-form.scss';
class VerificationCodeForm extends Component {
static propTypes = {
formUpdate: PropTypes.func.isRequired,
loginUserWithTwoFactorVerificationCode: PropTypes.func.isRequired,
onSuccess: PropTypes.func.isRequired,
recordTracksEvent: PropTypes.func.isRequired,
sendSmsCode: PropTypes.func.isRequired,
switchTwoFactorAuthType: PropTypes.func.isRequired,
translate: PropTypes.func.isRequired,
twoFactorAuthRequestError: PropTypes.object,
twoFactorAuthType: PropTypes.string.isRequired,
verificationCodeInputPlaceholder: PropTypes.string,
};
state = {
twoStepCode: '',
isDisabled: true,
};
componentDidMount() {
// eslint-disable-next-line react/no-did-mount-set-state
this.setState( { isDisabled: false }, () => {
this.input.focus();
} );
}
componentDidUpdate( prevProps ) {
const { twoFactorAuthRequestError, twoFactorAuthType } = this.props;
const hasNewError = ! prevProps.twoFactorAuthRequestError && twoFactorAuthRequestError;
const isNewPage = prevProps.twoFactorAuthType !== twoFactorAuthType;
if ( isNewPage || ( hasNewError && twoFactorAuthRequestError.field === 'twoStepCode' ) ) {
defer( () => this.input.focus() );
}
}
onChangeField = ( event ) => {
const { name, value = '' } = event.target;
this.props.formUpdate();
this.setState( { [ name ]: value } );
};
onSubmitForm = ( event ) => {
event.preventDefault();
const { onSuccess, twoFactorAuthType } = this.props;
const { twoStepCode } = this.state;
this.props.recordTracksEvent( 'calypso_login_two_factor_verification_code_submit' );
this.setState( { isDisabled: true } );
this.props
.loginUserWithTwoFactorVerificationCode( twoStepCode, twoFactorAuthType )
.then( () => {
this.props.recordTracksEvent( 'calypso_login_two_factor_verification_code_success' );
onSuccess();
} )
.catch( ( error ) => {
this.setState( { isDisabled: false } );
this.props.recordTracksEvent( 'calypso_login_two_factor_verification_code_failure', {
error_code: error.code,
error_message: error.message,
} );
} );
};
saveRef = ( input ) => {
this.input = input;
};
render() {
const {
translate,
twoFactorAuthRequestError: requestError,
twoFactorAuthType,
switchTwoFactorAuthType,
} = this.props;
let buttonText = translate( 'Continue' );
let helpText = translate( 'Enter the code from your authenticator app.' );
let labelText = translate( '6-Digit code' );
let smallPrint;
if ( twoFactorAuthType === 'sms' ) {
helpText = translate( 'Enter the code from the text message we sent you.' );
}
if ( twoFactorAuthType === 'backup' ) {
helpText = translate(
'Enter a backup code to continue. Use one of the 10 codes you received during setup.'
);
labelText = translate( 'Backup code' );
smallPrint = (
<div className="two-factor-authentication__small-print">
{ translate( 'Can’t access your backup codes? {{link}}Contact\u00A0support{{/link}}', {
components: {
link: (
<ActionPanelLink
href={ localizeUrl( 'https://wordpress.com/support/account-recovery/' ) }
/>
),
},
} ) }
</div>
);
buttonText = translate( 'Continue with backup code' );
}
return (
<form
className="two-factor-authentication__verification-code-form-wrapper"
onSubmit={ this.onSubmitForm }
>
<Card className="two-factor-authentication__verification-code-form">
<p className="verification-code-form__help-text">{ helpText }</p>
<FormFieldset>
<FormLabel htmlFor="twoStepCode">{ labelText }</FormLabel>
{ /* The use of `autoFocus` is intentional in this step. */ }
<FormVerificationCodeInput
autoFocus // eslint-disable-line jsx-a11y/no-autofocus
value={ this.state.twoStepCode }
onChange={ this.onChangeField }
isError={ requestError && requestError.field === 'twoStepCode' }
id="twoStepCode"
name="twoStepCode"
method={ twoFactorAuthType }
ref={ this.saveRef }
disabled={ this.state.isDisabled }
placeholder={ this.props.verificationCodeInputPlaceholder }
/>
{ requestError && requestError.field === 'twoStepCode' && (
<FormInputValidation isError text={ requestError.message } />
) }
{ smallPrint }
</FormFieldset>
<Button
type="submit"
variant="primary"
disabled={ this.state.isDisabled }
accessibleWhenDisabled
__next40pxDefaultSize
>
{ buttonText }
</Button>
</Card>
<TwoFactorActions
twoFactorAuthType={ twoFactorAuthType }
switchTwoFactorAuthType={ switchTwoFactorAuthType }
/>
</form>
);
}
}
export default connect(
( state ) => ( {
twoFactorAuthRequestError: getTwoFactorAuthRequestError( state ),
} ),
{
formUpdate,
loginUserWithTwoFactorVerificationCode,
recordTracksEvent,
sendSmsCode,
}
)( localize( VerificationCodeForm ) );
|