File size: 7,060 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { getTracksAnonymousUserId } from '@automattic/calypso-analytics';
import config from '@automattic/calypso-config';
import { ExternalLink } from '@automattic/components';
import { localizeUrl } from '@automattic/i18n-utils';
import { Notice } from '@wordpress/components';
import { addQueryArgs } from '@wordpress/url';
import { useTranslate } from 'i18n-calypso';
import { QRCodeSVG } from 'qrcode.react';
import { useEffect, useState } from 'react';
import qrCenter from 'calypso/assets/images/qr-login/app.png';
import { setStoredItem, getStoredItem } from 'calypso/lib/browser-storage';
import { useInterval } from 'calypso/lib/interval';
import { postLoginRequest, getErrorFromHTTPError } from 'calypso/state/login/utils';
import { JetpackQRCodeLogin } from './jetpack';

import './style.scss';

const AUTH_PULL_INTERVAL = 5000; // 5 seconds
const LOCALE_STORAGE_KEY = 'qr-login-token';

const isStillValidToken = ( tokenData ) => {
	if ( ! tokenData?.expires ) {
		return false;
	}
	return tokenData.expires > Date.now() / 1000;
};

const getLoginActionResponse = async ( action, args ) => {
	return postLoginRequest( action, {
		...args,
		client_id: config( 'wpcom_signup_id' ),
		client_secret: config( 'wpcom_signup_key' ),
	} )
		.then( ( response ) => {
			if ( response.ok ) {
				return response;
			}
			return response;
		} )
		.catch( ( httpError ) => {
			return Promise.reject( getErrorFromHTTPError( httpError ) );
		} );
};

export function TokenQRCode( { tokenData } ) {
	if ( ! tokenData ) {
		return <QRCodePlaceholder />;
	}
	const { token, encrypted } = tokenData;
	const imageSettings = {
		src: qrCenter,
		height: 64,
		width: 64,
		excavate: false,
	};
	return (
		<QRCodeSVG
			value={ localizeUrl(
				`https://apps.wordpress.com/get/?campaign=login-qr-code#qr-code-login?token=${ token }&data=${ encrypted }`
			) }
			size={ 300 }
			imageSettings={ imageSettings }
		/>
	);
}

function QRCodePlaceholder() {
	return (
		<div className="qr-code-login__placeholder">
			<span className="qr-code-login__corner-box"></span>
			<span className="qr-code-login__corner-box"></span>
			<span className="qr-code-login__corner-box"></span>
		</div>
	);
}

function QRCodeErrorCard() {
	const translate = useTranslate();

	return (
		<div className="qr-code-login is-error">
			<div className="qr-code-login__token-error">
				<h1 className="qr-code-login-page__heading">{ translate( 'Log in via Jetpack app' ) }</h1>
				<p>{ translate( 'Mobile App QR Code login is currently unavailable.' ) }</p>
			</div>
		</div>
	);
}

function QRCodeLogin( { redirectToAfterLoginUrl, isJetpack = false } ) {
	const translate = useTranslate();
	const [ tokenState, setTokenState ] = useState( null );
	const [ authState, setAuthState ] = useState( false );
	const [ isErrorState, setIsErrorState ] = useState( false );
	const [ pullInterval, setPullInterval ] = useState( AUTH_PULL_INTERVAL );

	const anonymousUserId = getTracksAnonymousUserId();

	const fetchQRCodeData = async ( tokenData, anonId ) => {
		if ( isStillValidToken( tokenData ) ) {
			return;
		}

		if ( ! anonId ) {
			return;
		}
		// tokenData is set to null initially.
		// Lets wait till it is set to false when the local data is the just yet.
		if ( tokenData === null ) {
			return;
		}

		try {
			const responseData = await getLoginActionResponse( 'qr-code-token-request-endpoint', {
				anon_id: anonId,
			} );
			if ( isStillValidToken( responseData.body.data ) ) {
				setTokenState( responseData.body.data );
				setStoredItem( LOCALE_STORAGE_KEY, responseData.body.data );
				return;
			}
		} catch {
			setIsErrorState( true );
			return;
		}
		setIsErrorState( true );
	};

	const fetchAuthState = async ( tokenData, anonId, isInError ) => {
		if ( ! tokenData ) {
			return;
		}

		if ( isInError ) {
			setPullInterval( null );
			return;
		}

		if ( ! anonymousUserId ) {
			return;
		}

		if ( ! isStillValidToken( tokenData ) ) {
			fetchQRCodeData( tokenData, anonId );
			return;
		}

		const { token, encrypted } = tokenData;
		try {
			const responseData = await getLoginActionResponse( 'qr-code-authentication-endpoint', {
				anon_id: anonId,
				token,
				data: encrypted,
			} );
			setAuthState( responseData.body.data );
		} catch ( error ) {
			if ( error.code !== 'auth_missing' ) {
				setIsErrorState( true );
				setPullInterval( null );
			}
			return;
		}
	};

	// Set the error state if we don't have a anonymousUserId
	useEffect( () => {
		if ( ! anonymousUserId ) {
			setIsErrorState( true );
		}
	}, [ anonymousUserId ] );

	// Fetch QR code data.
	useEffect( () => {
		fetchQRCodeData( tokenState, anonymousUserId );
	}, [ tokenState, anonymousUserId ] );

	// Fetch the Auth Data.
	useInterval( () => {
		fetchAuthState( tokenState, anonymousUserId, isErrorState );
	}, pullInterval );

	// Send the user to the login state.
	useEffect( () => {
		if ( authState?.auth_url ) {
			// if redirect URL is set, append to to the response URL as a query param.
			if ( redirectToAfterLoginUrl ) {
				authState.auth_url = addQueryArgs( authState.auth_url, {
					redirect_to: redirectToAfterLoginUrl,
				} );
			}
			// Clear the data.
			setStoredItem( LOCALE_STORAGE_KEY, null );
			window.location.replace( authState.auth_url );
		}
	}, [ authState, redirectToAfterLoginUrl ] );

	useEffect( () => {
		getStoredItem( LOCALE_STORAGE_KEY ).then( ( storedTokenData ) =>
			setTokenState( storedTokenData ?? false )
		);
	}, [] );

	const steps = [
		// translation: Link to the Jetpack App.
		translate( 'Open the {{link}}%(name)s App{{/link}} on your phone.', {
			args: {
				name: 'Jetpack',
			},
			components: {
				link: (
					<ExternalLink target="_blank" href="https://jetpack.com/app?campaign=login-qr-code" />
				),
			},
		} ),
		translate( 'Tap the {{strong}}Me{{/strong}} tab.', {
			components: {
				strong: <strong />,
			},
		} ),
		translate( 'Tap the {{strong}}Scan Login Code{{/strong}} option.', {
			components: {
				strong: <strong />,
			},
		} ),
		translate( 'Point your phone to this screen to scan the code.' ),
	];

	const notice = translate(
		'Logging in via the Jetpack app is {{strong}}not available{{/strong}} to accounts with two-factor authentication enabled.',
		{
			components: {
				strong: <strong />,
			},
		}
	);

	if ( isErrorState ) {
		return <QRCodeErrorCard />;
	}

	if ( isJetpack ) {
		return <JetpackQRCodeLogin tokenState={ tokenState } />;
	}

	return (
		<div className="qr-code-login">
			<div className="qr-code-login__token">
				<TokenQRCode tokenData={ tokenState } />
			</div>

			<div className="qr-code-login__instructions">
				<h1 className="qr-code-login-page__heading">{ translate( 'Log in via Jetpack app' ) }</h1>
				<Notice isDismissible={ false } status="warning">
					<p>{ notice }</p>
				</Notice>
				<ol className="qr-code-login__steps">
					{ steps.map( ( step, index ) => (
						<li key={ 'step-' + index } className="qr-code-login__step">
							{ step }
						</li>
					) ) }
				</ol>
			</div>
		</div>
	);
}

export default QRCodeLogin;