File size: 3,342 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
import { Card, Spinner } from '@automattic/components';
import { Button } from '@wordpress/components';
import clsx from 'clsx';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import { recordTracksEventWithClientId as recordTracksEvent } from 'calypso/state/analytics/actions';
import { formUpdate, loginUserWithSecurityKey } from 'calypso/state/login/actions';
import TwoFactorActions from './two-factor-actions';
import './verification-code-form.scss';
import './security-key-form.scss';

class SecurityKeyForm extends Component {
	static propTypes = {
		formUpdate: PropTypes.func.isRequired,
		loginUserWithSecurityKey: PropTypes.func.isRequired,
		onSuccess: PropTypes.func.isRequired,
		recordTracksEvent: PropTypes.func.isRequired,
		switchTwoFactorAuthType: PropTypes.func.isRequired,
		translate: PropTypes.func.isRequired,
		showOrDivider: PropTypes.bool,
		isWoo: PropTypes.bool,
	};

	static defaultProps = {
		isWoo: false,
	};

	state = {
		isAuthenticating: false,
	};

	componentDidMount() {
		this.initiateSecurityKeyAuthentication();
	}

	initiateSecurityKeyAuthentication = () => {
		const { onSuccess } = this.props;
		this.setState( { isAuthenticating: true } );
		this.props
			.loginUserWithSecurityKey()
			.then( () => onSuccess() )
			.catch( () => this.setState( { isAuthenticating: false } ) );
	};

	render() {
		const { translate, isWoo, switchTwoFactorAuthType } = this.props;

		return (
			<form
				className={ clsx( 'two-factor-authentication__verification-code-form-wrapper', {
					isWoo: isWoo,
				} ) }
				onSubmit={ ( event ) => {
					event.preventDefault();
					this.initiateSecurityKeyAuthentication();
				} }
			>
				<Card className="two-factor-authentication__verification-code-form">
					{ ! this.state.isAuthenticating && (
						<div className="security-key-form__help-text">
							<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>
					) }
					{ this.state.isAuthenticating && (
						<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>
					) }
					<Button
						// eslint-disable-next-line jsx-a11y/no-autofocus
						autoFocus
						variant="primary"
						disabled={ this.state.isAuthenticating }
						type="submit"
						accessibleWhenDisabled
						__next40pxDefaultSize
					>
						{ translate( 'Continue with security key' ) }
					</Button>
				</Card>

				<TwoFactorActions
					twoFactorAuthType="webauthn"
					switchTwoFactorAuthType={ switchTwoFactorAuthType }
				/>
			</form>
		);
	}
}

export default connect( null, {
	formUpdate,
	loginUserWithSecurityKey,
	recordTracksEvent,
} )( localize( SecurityKeyForm ) );