Skip to content

Commit 9cca958

Browse files
committed
Revert the change to credentials after dismissing Jetpack setup to avoid edge cases
1 parent 36f3288 commit 9cca958

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

WooCommerce/Classes/ViewRelated/JetpackSetup/JetpackSetupCoordinator.swift

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,7 @@ private extension JetpackSetupCoordinator {
118118
requiresConnectionOnly: requiresConnectionOnly
119119
))
120120
if let connectedEmail = jetpackConnectedEmail {
121-
/// prevent asking for authentication again if the user starts Jetpack setup again after completion.
122-
if stores.isAuthenticated,
123-
stores.isAuthenticatedWithoutWPCom == false,
124-
let credentials = stores.sessionManager.defaultCredentials {
125-
refreshSite(with: credentials)
126-
} else {
127-
startAuthentication(with: connectedEmail)
128-
}
121+
startAuthentication(with: connectedEmail)
129122
} else {
130123
showWPComEmailLogin()
131124
}
@@ -258,19 +251,17 @@ private extension JetpackSetupCoordinator {
258251

259252
func authenticateUserAndRefreshSite(with credentials: Credentials) {
260253
analytics.track(.jetpackSetupCompleted)
261-
stores.sessionManager.deleteApplicationPassword()
254+
let previousCredentials = stores.sessionManager.defaultCredentials
262255
stores.authenticate(credentials: credentials)
263-
refreshSite(with: credentials)
264-
}
265256

266-
func refreshSite(with credentials: Credentials) {
267257
let progressView = InProgressViewController(viewProperties: .init(title: Localization.syncingData, message: ""))
268258
rootViewController.topmostPresentedViewController.present(progressView, animated: true)
269259

270260
let action = SiteAction.syncSiteByDomain(domain: site.url.trimHTTPScheme()) { [weak self] result in
271261
guard let self else { return }
272262
switch result {
273263
case .success(let site):
264+
self.stores.sessionManager.deleteApplicationPassword()
274265
self.stores.updateDefaultStore(storeID: site.siteID)
275266
self.stores.synchronizeEntities { [weak self] in
276267
self?.stores.updateDefaultStore(site)
@@ -288,6 +279,11 @@ private extension JetpackSetupCoordinator {
288279
progressView.dismiss(animated: true, completion: { [weak self] in
289280
self?.showAlert(message: Localization.errorFetchingSites, onRetry: {
290281
self?.authenticateUserAndRefreshSite(with: credentials)
282+
}, onCancel: {
283+
// Revert the change to credentials
284+
if let previousCredentials {
285+
self?.stores.authenticate(credentials: previousCredentials)
286+
}
291287
})
292288
})
293289

@@ -447,7 +443,8 @@ private extension JetpackSetupCoordinator {
447443
/// Shows an error alert with a button to retry the failed action.
448444
///
449445
func showAlert(message: String,
450-
onRetry: (() -> Void)? = nil) {
446+
onRetry: (() -> Void)? = nil,
447+
onCancel: (() -> Void)? = nil) {
451448
let alert = UIAlertController(title: message,
452449
message: nil,
453450
preferredStyle: .alert)
@@ -457,7 +454,9 @@ private extension JetpackSetupCoordinator {
457454
}
458455
alert.addAction(retryAction)
459456
}
460-
let cancelAction = UIAlertAction(title: Localization.cancelButton, style: .cancel)
457+
let cancelAction = UIAlertAction(title: Localization.cancelButton, style: .cancel) { _ in
458+
onCancel?()
459+
}
461460
alert.addAction(cancelAction)
462461
rootViewController.topmostPresentedViewController.present(alert, animated: true)
463462
}

0 commit comments

Comments
 (0)