File size: 3,204 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
import { Card, FormInputValidation, Spinner } from '@automattic/components';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import FormButton from 'calypso/components/forms/form-button';
import { getCurrentUserId } from 'calypso/state/current-user/selectors';

import './security-key-form.scss';

export class SecurityKeyForm extends Component {
	static propTypes = {
		twoStepAuthorization: PropTypes.object.isRequired,
		translate: PropTypes.func.isRequired,
	};

	state = {
		isAuthenticating: false,
		showError: false,
	};

	componentDidMount() {
		this.initiateSecurityKeyAuthentication();
	}

	initiateSecurityKeyAuthentication = ( retryRequest = true ) => {
		this.setState( { isAuthenticating: true, showError: false } );

		this.props.twoStepAuthorization
			.loginUserWithSecurityKey( { user_id: this.props.currentUserId } )
			.catch( ( error ) => {
				const errors = error?.data?.errors ?? [];
				if ( errors.some( ( e ) => e.code === 'invalid_two_step_nonce' ) ) {
					this.props.twoStepAuthorization.fetch( () => {
						if ( retryRequest ) {
							this.initiateSecurityKeyAuthentication( false );
						} else {
							// We only retry once, so let's show the original error.
							this.setState( { isAuthenticating: false, showError: true } );
						}
					} );
					return;
				}
				this.setState( { isAuthenticating: false, showError: true } );
			} );
	};

	render() {
		const { translate } = this.props;
		const { isAuthenticating } = this.state;

		return (
			<form
				onSubmit={ ( event ) => {
					event.preventDefault();
					this.initiateSecurityKeyAuthentication();
				} }
			>
				<Card compact className="security-key-form__verification-code-form">
					{ ! isAuthenticating ? (
						<div>
							<p>
								{ translate( '{{strong}}Use your security key to finish logging in.{{/strong}}', {
									components: {
										strong: <strong />,
									},
								} ) }
							</p>
							<p>
								{ translate(
									'Insert your hardware security key, or follow the instructions in your browser or phone to log in.'
								) }
							</p>
						</div>
					) : (
						<div className="security-key-form__add-wait-for-key">
							<Spinner />
							<p className="security-key-form__add-wait-for-key-heading">
								{ translate( 'Waiting for security key' ) }
							</p>
							<p>
								{ translate(
									'Connect and touch your security key to log in, or follow the directions in your browser or pop-up.'
								) }
							</p>
						</div>
					) }
					{ this.state.showError && (
						<FormInputValidation
							isError
							text={ this.props.translate(
								'An error occurred, please try again or use an alternate authentication method.'
							) }
						/>
					) }
					<FormButton
						autoFocus // eslint-disable-line jsx-a11y/no-autofocus
						primary
						disabled={ isAuthenticating }
					>
						{ translate( 'Continue with security key' ) }
					</FormButton>
				</Card>
			</form>
		);
	}
}

export default connect( ( state ) => ( {
	currentUserId: getCurrentUserId( state ),
} ) )( localize( SecurityKeyForm ) );