File size: 4,792 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
import { isEnabled } from '@automattic/calypso-config';
import page from '@automattic/calypso-router';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import DocumentHead from 'calypso/components/data/document-head';
import QueryUserSettings from 'calypso/components/data/query-user-settings';
import HeaderCake from 'calypso/components/header-cake';
import Main from 'calypso/components/main';
import NavigationHeader from 'calypso/components/navigation-header';
import PageViewTracker from 'calypso/lib/analytics/page-view-tracker';
import twoStepAuthorization from 'calypso/lib/two-step-authorization';
import AppPasswords from 'calypso/me/application-passwords';
import ReauthRequired from 'calypso/me/reauth-required';
import Security2faBackupCodes from 'calypso/me/security-2fa-backup-codes';
import Security2faDisable from 'calypso/me/security-2fa-disable';
import Security2faKey from 'calypso/me/security-2fa-key';
import Security2faSetup from 'calypso/me/security-2fa-setup';
import SecuritySectionNav from 'calypso/me/security-section-nav';
import { isCurrentUserEmailVerified } from 'calypso/state/current-user/selectors';
import getUserSettings from 'calypso/state/selectors/get-user-settings';
import isTwoStepEnabled from 'calypso/state/selectors/is-two-step-enabled';
import { fetchUserSettings } from 'calypso/state/user-settings/actions';
import { isFetchingUserSettings } from 'calypso/state/user-settings/selectors';
import Security2faEnhancedSecuritySetting from '../security-2fa-enhanced-security-setting';

import './style.scss';

class TwoStep extends Component {
	static propTypes = {
		translate: PropTypes.func.isRequired,
	};

	onSetupFinished = () => {
		this.props.fetchUserSettings();
	};

	onDisableFinished = () => {
		this.props.fetchUserSettings();
	};

	componentDidMount() {
		if ( ! this.props.isFetchingUserSettings && ! this.props.isEmailVerified ) {
			page.redirect( '/me/security' );
		}
	}

	componentDidUpdate() {
		if ( ! this.props.isFetchingUserSettings && ! this.props.isEmailVerified ) {
			page.redirect( '/me/security' );
		}
	}

	renderPlaceholders = () => {
		const placeholders = [];

		for ( let i = 0; i < 5; i++ ) {
			placeholders.push(
				<p className="two-step__placeholder-text" key={ '2fa-placeholder' + i }>
					{ ' ' }
					&nbsp;{ ' ' }
				</p>
			);
		}

		return placeholders;
	};

	renderTwoStepSection = () => {
		if ( this.props.isFetchingUserSettings ) {
			return this.renderPlaceholders();
		}

		if ( ! this.props.isTwoStepEnabled ) {
			return <Security2faSetup onFinished={ this.onSetupFinished } />;
		}

		return <Security2faDisable onFinished={ this.onDisableFinished } />;
	};

	renderApplicationPasswords = () => {
		if ( this.props.isFetchingUserSettings || ! this.props.isTwoStepEnabled ) {
			return null;
		}

		return <AppPasswords />;
	};

	render2faKey = () => {
		if ( this.props.isFetchingUserSettings || ! this.props.isTwoStepEnabled ) {
			return null;
		}

		return <Security2faKey />;
	};

	renderBackupCodes = () => {
		if ( this.props.isFetchingUserSettings || ! this.props.isTwoStepEnabled ) {
			return null;
		}

		return <Security2faBackupCodes />;
	};

	renderEnhancedSecuritySetting = () => {
		if (
			! isEnabled( 'two-factor/enhanced-security' ) ||
			this.props.isFetchingUserSettings ||
			! this.props.isTwoStepEnabled
		) {
			return null;
		}
		return <Security2faEnhancedSecuritySetting />;
	};

	render() {
		const { path, translate } = this.props;
		const useCheckupMenu = isEnabled( 'security/security-checkup' );

		return (
			<Main wideLayout className="security two-step">
				<QueryUserSettings />
				<PageViewTracker path="/me/security/two-step" title="Me > Two-Step Authentication" />

				<ReauthRequired twoStepAuthorization={ twoStepAuthorization } />

				<DocumentHead title={ translate( 'Two-Step Authentication' ) } />

				<NavigationHeader navigationItems={ [] } title={ translate( 'Security' ) } />

				{ ! useCheckupMenu && <SecuritySectionNav path={ path } /> }
				{ useCheckupMenu && (
					<HeaderCake backText={ translate( 'Back' ) } backHref="/me/security">
						{ translate( 'Two-Step Authentication' ) }
					</HeaderCake>
				) }

				{ this.renderTwoStepSection() }
				{ this.renderEnhancedSecuritySetting() }
				{ this.render2faKey() }
				{ this.renderBackupCodes() }
				{ this.renderApplicationPasswords() }
			</Main>
		);
	}
}

export default connect(
	( state ) => ( {
		isFetchingUserSettings: isFetchingUserSettings( state ),
		userSettings: getUserSettings( state ),
		isTwoStepEnabled: isTwoStepEnabled( state ),
		isEmailVerified: isCurrentUserEmailVerified( state ),
	} ),
	{
		fetchUserSettings,
	}
)( localize( TwoStep ) );