Skip to content

Instantly share code, notes, and snippets.

@yurynix
Created July 20, 2017 11:27
Show Gist options
  • Save yurynix/c24fa7c7e9ade4c167e4f026ab6a8e67 to your computer and use it in GitHub Desktop.
Save yurynix/c24fa7c7e9ade4c167e4f026ab6a8e67 to your computer and use it in GitHub Desktop.
diff --git a/client/state/action-types.js b/client/state/action-types.js
index 04e4050de8..7e5219e66d 100644
--- a/client/state/action-types.js
+++ b/client/state/action-types.js
@@ -802,9 +802,8 @@ export const TIMEZONES_REQUEST_SUCCESS = 'TIMEZONES_REQUEST_SUCCESS';
export const TWO_FACTOR_AUTHENTICATION_LOGIN_REQUEST = 'TWO_FACTOR_AUTHENTICATION_LOGIN_REQUEST';
export const TWO_FACTOR_AUTHENTICATION_LOGIN_REQUEST_FAILURE = 'TWO_FACTOR_AUTHENTICATION_LOGIN_REQUEST_FAILURE';
export const TWO_FACTOR_AUTHENTICATION_LOGIN_REQUEST_SUCCESS = 'TWO_FACTOR_AUTHENTICATION_LOGIN_REQUEST_SUCCESS';
-export const TWO_FACTOR_AUTHENTICATION_PUSH_POLL_COMPLETED = 'TWO_FACTOR_AUTHENTICATION_PUSH_POLL_COMPLETED';
-export const TWO_FACTOR_AUTHENTICATION_PUSH_POLL_START = 'TWO_FACTOR_AUTHENTICATION_PUSH_POLL_START';
-export const TWO_FACTOR_AUTHENTICATION_PUSH_POLL_STOP = 'TWO_FACTOR_AUTHENTICATION_PUSH_POLL_STOP';
+export const TWO_FACTOR_AUTHENTICATION_PUSH_POLL = 'TWO_FACTOR_AUTHENTICATION_PUSH_POLL';
+export const TWO_FACTOR_AUTHENTICATION_PUSH_CANCEL = 'TWO_FACTOR_AUTHENTICATION_PUSH_CANCEL';
export const TWO_FACTOR_AUTHENTICATION_SEND_SMS_CODE_REQUEST = 'TWO_FACTOR_AUTHENTICATION_SEND_SMS_CODE_REQUEST';
export const TWO_FACTOR_AUTHENTICATION_SEND_SMS_CODE_REQUEST_FAILURE = 'TWO_FACTOR_AUTHENTICATION_SEND_SMS_CODE_REQUEST_FAILURE';
export const TWO_FACTOR_AUTHENTICATION_SEND_SMS_CODE_REQUEST_SUCCESS = 'TWO_FACTOR_AUTHENTICATION_SEND_SMS_CODE_REQUEST_SUCCESS';
diff --git a/client/state/data-layer/wpcom/login-2fa/index.js b/client/state/data-layer/wpcom/login-2fa/index.js
index e6050e9e08..2dc6953387 100644
--- a/client/state/data-layer/wpcom/login-2fa/index.js
+++ b/client/state/data-layer/wpcom/login-2fa/index.js
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
-import { get } from 'lodash';
+import { get, merge } from 'lodash';
/**
* Internal dependencies
@@ -9,9 +9,8 @@ import { get } from 'lodash';
import config from 'config';
import {
TWO_FACTOR_AUTHENTICATION_UPDATE_NONCE,
- TWO_FACTOR_AUTHENTICATION_PUSH_POLL_COMPLETED,
- TWO_FACTOR_AUTHENTICATION_PUSH_POLL_START,
- TWO_FACTOR_AUTHENTICATION_PUSH_POLL_STOP,
+ TWO_FACTOR_AUTHENTICATION_PUSH_POLL,
+ TWO_FACTOR_AUTHENTICATION_PUSH_CANCEL,
} from 'state/action-types';
import {
getRememberMe,
@@ -54,8 +53,9 @@ const requestTwoFactorPushNotificationStatus = ( store, action ) => {
) );
};
-const receivedTwoFactorPushNotificationApproved = ( { dispatch } ) =>
- dispatch( { type: TWO_FACTOR_AUTHENTICATION_PUSH_POLL_COMPLETED } );
+const receivedTwoFactorPushNotificationApproved = ( { dispatch }, action ) => dispatch( action );
+
+let cancelPolling = false;
/***
* Receive error from the two factor push notification status http request
@@ -66,9 +66,9 @@ const receivedTwoFactorPushNotificationApproved = ( { dispatch } ) =>
*/
const receivedTwoFactorPushNotificationError = ( store, action, next, error ) => {
const twoStepNonce = get( error, 'response.body.data.two_step_nonce' );
+ const retryCount = get( action, 'meta.retryCount', 0 );
if ( ! twoStepNonce ) {
- store.dispatch( { type: TWO_FACTOR_AUTHENTICATION_PUSH_POLL_STOP } );
throw new Error( "Two step nonce wasn't present on the response from polling endpoint, unable to continue" );
}
@@ -78,25 +78,29 @@ const receivedTwoFactorPushNotificationError = ( store, action, next, error ) =>
twoStepNonce,
} );
- if ( getTwoFactorPushPollInProgress( store.getState() ) ) {
- setTimeout(
- // eslint-disable-next-line no-use-before-define
- () => makePushNotificationRequest( store, { type: action.type } ),
- POLL_APP_PUSH_INTERVAL_SECONDS * 1000
- );
- }
+
+ setTimeout(
+ // eslint-disable-next-line no-use-before-define
+ () => ! cancelPolling && store.dispatch( merge( {}, action, { meta: { retryCount: 1 + retryCount } } ) ),
+ POLL_APP_PUSH_INTERVAL_SECONDS * 1000
+ );
};
-const makePushNotificationRequest = dispatchRequest(
- requestTwoFactorPushNotificationStatus,
- receivedTwoFactorPushNotificationApproved,
- receivedTwoFactorPushNotificationError,
-);
export default {
- [ TWO_FACTOR_AUTHENTICATION_PUSH_POLL_START ]: [ ( store, action ) => {
- // We need to store to update for `getTwoFactorPushPollInProgress` selector
- store.dispatch( local( action ) );
- return makePushNotificationRequest( store, action );
+ [ TWO_FACTOR_AUTHENTICATION_PUSH_POLL ]: [ ( store, action ) => {
+ // if it's a "fresh" action, reset cancel flag
+ if ( ! get( action, 'meta.retryCount', false ) && cancelPolling ) {
+ cancelPolling = false;
+ }
+
+ return dispatchRequest(
+ requestTwoFactorPushNotificationStatus,
+ receivedTwoFactorPushNotificationApproved,
+ receivedTwoFactorPushNotificationError,
+ );
+ } ],
+ [ TWO_FACTOR_AUTHENTICATION_PUSH_CANCEL ]: [ () => {
+ cancelPolling = true;
} ],
};
diff --git a/client/state/login/actions.js b/client/state/login/actions.js
index 6f125fe758..3d4ef494dc 100644
--- a/client/state/login/actions.js
+++ b/client/state/login/actions.js
@@ -22,8 +22,8 @@ import {
TWO_FACTOR_AUTHENTICATION_LOGIN_REQUEST,
TWO_FACTOR_AUTHENTICATION_LOGIN_REQUEST_FAILURE,
TWO_FACTOR_AUTHENTICATION_LOGIN_REQUEST_SUCCESS,
- TWO_FACTOR_AUTHENTICATION_PUSH_POLL_START,
- TWO_FACTOR_AUTHENTICATION_PUSH_POLL_STOP,
+ TWO_FACTOR_AUTHENTICATION_PUSH_POLL,
+ TWO_FACTOR_AUTHENTICATION_PUSH_POLL_CANCEL,
TWO_FACTOR_AUTHENTICATION_SEND_SMS_CODE_REQUEST,
TWO_FACTOR_AUTHENTICATION_SEND_SMS_CODE_REQUEST_FAILURE,
TWO_FACTOR_AUTHENTICATION_SEND_SMS_CODE_REQUEST_SUCCESS,
@@ -331,5 +331,5 @@ export const sendSmsCode = () => ( dispatch, getState ) => {
} );
};
-export const startPollAppPushAuth = () => ( { type: TWO_FACTOR_AUTHENTICATION_PUSH_POLL_START } );
-export const stopPollAppPushAuth = () => ( { type: TWO_FACTOR_AUTHENTICATION_PUSH_POLL_STOP } );
+export const startPollAppPushAuth = () => ( { type: TWO_FACTOR_AUTHENTICATION_PUSH_POLL } );
+export const stopPollAppPushAuth = () => ( { type: TWO_FACTOR_AUTHENTICATION_PUSH_POLL_CANCEL } );
diff --git a/client/state/login/reducer.js b/client/state/login/reducer.js
index b0a0d7d488..0c79866c9d 100644
--- a/client/state/login/reducer.js
+++ b/client/state/login/reducer.js
@@ -22,9 +22,7 @@ import {
TWO_FACTOR_AUTHENTICATION_LOGIN_REQUEST,
TWO_FACTOR_AUTHENTICATION_LOGIN_REQUEST_FAILURE,
TWO_FACTOR_AUTHENTICATION_LOGIN_REQUEST_SUCCESS,
- TWO_FACTOR_AUTHENTICATION_PUSH_POLL_COMPLETED,
- TWO_FACTOR_AUTHENTICATION_PUSH_POLL_START,
- TWO_FACTOR_AUTHENTICATION_PUSH_POLL_STOP,
+ TWO_FACTOR_AUTHENTICATION_PUSH_POLL,
TWO_FACTOR_AUTHENTICATION_SEND_SMS_CODE_REQUEST,
TWO_FACTOR_AUTHENTICATION_SEND_SMS_CODE_REQUEST_FAILURE,
TWO_FACTOR_AUTHENTICATION_SEND_SMS_CODE_REQUEST_SUCCESS,
@@ -120,9 +118,7 @@ export const twoFactorAuthRequestError = createReducer( null, {
} );
export const twoFactorAuthPushPoll = createReducer( { inProgress: false, success: false }, {
- [ TWO_FACTOR_AUTHENTICATION_PUSH_POLL_START ]: state => ( { ...state, inProgress: true, success: false } ),
- [ TWO_FACTOR_AUTHENTICATION_PUSH_POLL_STOP ]: state => ( { ...state, inProgress: false } ),
- [ TWO_FACTOR_AUTHENTICATION_PUSH_POLL_COMPLETED ]: state => ( { ...state, inProgress: false, success: true } ),
+ [ TWO_FACTOR_AUTHENTICATION_PUSH_POLL ]: state => ( { inProgress: false, success: true } ),
} );
export const socialAccount = createReducer( { isCreating: false }, {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment