Skip to content

Commit 3856427

Browse files
authored
Rewrite WPAuthTokenIssueSolver (#24397)
1 parent 0501264 commit 3856427

File tree

8 files changed

+76
-169
lines changed

8 files changed

+76
-169
lines changed

Sources/Keystone/Keystone.h

-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ FOUNDATION_EXPORT const unsigned char KeystoneVersionString[];
7373
#import <Keystone/WPAccount.h>
7474
#import <Keystone/WPActivityDefaults.h>
7575
#import <Keystone/WPAppAnalytics.h>
76-
#import <Keystone/WPAuthTokenIssueSolver.h>
7776
#import <Keystone/WPUploadStatusButton.h>
7877
#import <Keystone/WPError.h>
7978
#import <Keystone/WPTableViewHandler.h>

Sources/WordPressAuthenticator/Helpers/Authenticator/WordPressAuthenticator.swift

-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@ import WordPressKit
341341
}
342342
}
343343

344-
// Helper used by WPAuthTokenIssueSolver
345344
@objc
346345
public class func signinForWPCom(dotcomEmailAddress: String?, dotcomUsername: String?, onDismissed: ((_ cancelled: Bool) -> Void)? = nil) -> UIViewController {
347346
let loginFields = LoginFields()

WordPress/Classes/System/WordPress-Bridging-Header.h

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
#import "WPAccount.h"
6666
#import "WPActivityDefaults.h"
6767
#import "WPAppAnalytics.h"
68-
#import "WPAuthTokenIssueSolver.h"
6968
#import "WPUploadStatusButton.h"
7069
#import "WPError.h"
7170
#import "WPTableViewHandler.h"

WordPress/Classes/System/WordPressAppDelegate.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,8 @@ public class WordPressAppDelegate: UIResponder, UIApplicationDelegate {
104104
customizeAppearance()
105105
configureAnalytics()
106106

107-
let solver = WPAuthTokenIssueSolver()
108-
_ = solver.fixAuthTokenIssueAndDo { [weak self] in
109-
self?.runStartupSequence(with: launchOptions ?? [:])
107+
AuthTokenIssueSolver().fixAuthTokenIssueIfNeeded(in: window) {
108+
self.runStartupSequence(with: launchOptions ?? [:])
110109
}
111110

112111
return true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import UIKit
2+
import WordPressData
3+
4+
struct AuthTokenIssueSolver {
5+
private let coreData: CoreDataStack
6+
7+
init(coreData: CoreDataStack = ContextManager.shared) {
8+
self.coreData = coreData
9+
}
10+
11+
/// - note: The completion callback is run immediatelly if there is no issue.
12+
func fixAuthTokenIssueIfNeeded(in window: UIWindow, _ completion: @escaping () -> Void) {
13+
guard hasAuthTokenIssues() else {
14+
completion()
15+
return
16+
}
17+
18+
let signInVC = WordPressAuthenticationManager.signinForWPComFixingAuthToken { cancelled in
19+
if cancelled {
20+
// We present asynchronously to prevent an issue where the Login VC would dismiss the
21+
// alert instead of itself.
22+
DispatchQueue.main.async {
23+
showCancelReAuthenticationAlert(in: window, onDeletionConfirmed: {
24+
let accountService = AccountService(coreDataStack: ContextManager.sharedInstance())
25+
accountService.removeDefaultWordPressComAccount()
26+
completion()
27+
})
28+
}
29+
} else {
30+
completion()
31+
}
32+
}
33+
34+
window.rootViewController = signInVC
35+
36+
showExplanationAlertForReAuthentication(in: signInVC)
37+
}
38+
39+
/// Call this method to know if the local installation of WPiOS has the
40+
/// authToken issue this class was designed to solve.
41+
/// - returns: `true` if the local WPiOS installation needs to be fixed by this class.
42+
private func hasAuthTokenIssues() -> Bool {
43+
guard let account = try? WPAccount.lookupDefaultWordPressComAccount(in: coreData.mainContext) else {
44+
return false
45+
}
46+
return account.authToken == nil
47+
}
48+
49+
private func showCancelReAuthenticationAlert(in window: UIWindow, onDeletionConfirmed: @escaping () -> Void) {
50+
let title = NSLocalizedString("Careful!", comment: "Title for the warning shown to the user when he refuses to re-login when the authToken is missing.")
51+
let message = NSLocalizedString("Proceeding will remove all WordPress.com data from this device, and delete any locally saved drafts. You will not lose anything already saved to your WordPress.com blog(s).", comment: "Message for the warning shown to the user when he refuses to re-login when the authToken is missing.")
52+
53+
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
54+
55+
alert.addAction(UIAlertAction(title: SharedStrings.Button.cancel, style: .cancel) { _ in })
56+
alert.addAction(UIAlertAction(title: SharedStrings.Button.delete, style: .destructive) { _ in
57+
onDeletionConfirmed()
58+
})
59+
60+
window.rootViewController?.present(alert, animated: true, completion: nil)
61+
}
62+
63+
private func showExplanationAlertForReAuthentication(in presentingViewController: UIViewController) {
64+
let title = NSLocalizedString("Oops!", comment: "Title for the warning shown to the user when the app realizes there should be an auth token but there isn't one.")
65+
let message = NSLocalizedString("There was a problem connecting to WordPress.com. Please log in again.", comment: "Message for the warning shown to the user when the app realizes there should be an auth token but there isn't one.")
66+
67+
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
68+
69+
alert.addAction(UIAlertAction(title: SharedStrings.Button.ok, style: .default) { _ in })
70+
alert.modalPresentationStyle = .popover
71+
72+
presentingViewController.present(alert, animated: true)
73+
}
74+
}

WordPress/Classes/Utility/WPAuthTokenIssueSolver.h

-24
This file was deleted.

WordPress/Classes/Utility/WPAuthTokenIssueSolver.m

-138
This file was deleted.

WordPress/WordPress.xcodeproj/project.pbxproj

-1
Original file line numberDiff line numberDiff line change
@@ -1977,7 +1977,6 @@
19771977
Utility/Logging/WPLogger.h,
19781978
Utility/Sharing/WPActivityDefaults.h,
19791979
Utility/UIAlertControllerProxy.h,
1980-
Utility/WPAuthTokenIssueSolver.h,
19811980
Utility/WPError.h,
19821981
Utility/WPTableViewHandler.h,
19831982
"ViewRelated/Blog/Blog Details/BlogDetailsViewController.h",

0 commit comments

Comments
 (0)