File size: 3,506 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
import { Card, CompactCard } from '@automattic/components';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import Security2faEnable from 'calypso/me/security-2fa-enable';
import Security2faInitialSetup from 'calypso/me/security-2fa-initial-setup';
import Security2faSetupBackupCodes from 'calypso/me/security-2fa-setup-backup-codes';
import Security2faSMSSettings from 'calypso/me/security-2fa-sms-settings';
import { successNotice } from 'calypso/state/notices/actions';

export const SMS_BASED_2FA_SETUP_ENTER_PHONE_STEP = 'sms-settings';
export const SMS_BASED_2FA_SETUP_VALIDATE_CODE_STEP = 'sms-based';
export const APP_BASED_2FA_VALIDATE_STEP = 'app-based';
export const DISPLAY_BACKUP_CODES_STEP = 'backup-codes';
export const INITIAL_SETUP_STEP = 'initial-setup';

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

	state = {
		step: INITIAL_SETUP_STEP,
		authMethod: APP_BASED_2FA_VALIDATE_STEP,
	};

	onCancelSetup = ( event ) => {
		event.preventDefault();
		this.setState( { step: INITIAL_SETUP_STEP } );
	};

	onInitialSetupSuccess = ( event, authMethod ) => {
		this.setState( { step: authMethod, authMethod } );
	};

	onSetupSuccess = () => {
		this.setState( { step: DISPLAY_BACKUP_CODES_STEP } );
	};

	onFinished = () => {
		this.props.successNotice(
			this.props.translate( 'Successfully enabled Two-Step Authentication.' ),
			{
				duration: 4000,
			}
		);
		this.props.onFinished();
	};

	onVerifyByApp = () => {
		this.setState( { step: APP_BASED_2FA_VALIDATE_STEP } );
	};

	onVerifyBySMS = () => {
		this.setState( { step: SMS_BASED_2FA_SETUP_VALIDATE_CODE_STEP } );
	};

	render() {
		const { step, authMethod } = this.state;
		const { translate } = this.props;

		const isSmsFlow = [
			SMS_BASED_2FA_SETUP_VALIDATE_CODE_STEP,
			SMS_BASED_2FA_SETUP_ENTER_PHONE_STEP,
		].includes( authMethod );

		if ( step === INITIAL_SETUP_STEP ) {
			return (
				<div className="security-2fa-setup__steps-container">
					<Security2faInitialSetup onSuccess={ this.onInitialSetupSuccess } />
				</div>
			);
		}

		let title = '';
		let content = null;

		switch ( step ) {
			case SMS_BASED_2FA_SETUP_ENTER_PHONE_STEP:
				title = translate( 'Enter phone number' );
				content = (
					<Security2faSMSSettings
						onCancel={ this.onCancelSetup }
						onVerifyByApp={ this.onVerifyByApp }
						onVerifyBySMS={ this.onVerifyBySMS }
					/>
				);
				break;

			case APP_BASED_2FA_VALIDATE_STEP:
				title = translate( 'Verify code' );
				content = (
					<Security2faEnable
						isSmsFlow={ false }
						onCancel={ this.onCancelSetup }
						onSuccess={ this.onSetupSuccess }
					/>
				);
				break;

			case SMS_BASED_2FA_SETUP_VALIDATE_CODE_STEP:
				title = translate( 'Verify code' );
				content = (
					<Security2faEnable
						isSmsFlow
						onCancel={ this.onCancelSetup }
						onSuccess={ this.onSetupSuccess }
					/>
				);
				break;

			case DISPLAY_BACKUP_CODES_STEP:
				title = translate( 'Generate backup codes' );
				content = (
					<Security2faSetupBackupCodes isSmsFlow={ isSmsFlow } onFinished={ this.onFinished } />
				);
				break;

			default:
				return null;
		}

		return (
			<>
				<CompactCard>{ title }</CompactCard>
				<Card>{ content }</Card>
			</>
		);
	}
}

export default connect( null, { successNotice } )( localize( Security2faSetup ) );