Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.

Commit 075d308

Browse files
committed
Clean up code for finishedLogin
1 parent 061442b commit 075d308

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

WordPressAuthenticator/Unified Auth/View Related/Site Address/SiteCredentialsViewController.swift

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,38 @@ private extension SiteCredentialsViewController {
479479
displayError(error as NSError, sourceTag: sourceTag)
480480
}
481481
}
482+
483+
func syncDataOrPresentWPComLogin(with wporgCredentials: WordPressOrgCredentials) {
484+
if configuration.isWPComLoginRequiredForSiteCredentialsLogin {
485+
presentWPComLogin(wporgCredentials: wporgCredentials)
486+
return
487+
}
488+
// Client didn't explicitly ask for WPCOM credentials. (`isWPComLoginRequiredForSiteCredentialsLogin` is false)
489+
// So, sync the available credentials and finish sign in.
490+
//
491+
let credentials = AuthenticatorCredentials(wporg: wporgCredentials)
492+
WordPressAuthenticator.shared.delegate?.sync(credentials: credentials) { [weak self] in
493+
NotificationCenter.default.post(name: Foundation.Notification.Name(rawValue: WordPressAuthenticator.WPSigninDidFinishNotification), object: nil)
494+
self?.showLoginEpilogue(for: credentials)
495+
}
496+
}
497+
498+
func presentWPComLogin(wporgCredentials: WordPressOrgCredentials) {
499+
// Try to get the jetpack email from XML-RPC response dictionary.
500+
//
501+
guard let loginFields = makeLoginFieldsUsing(xmlrpc: wporgCredentials.xmlrpc,
502+
options: wporgCredentials.options) else {
503+
WPAuthenticatorLogError("Unexpected response from .org site credentials sign in using XMLRPC.")
504+
let credentials = AuthenticatorCredentials(wporg: wporgCredentials)
505+
showLoginEpilogue(for: credentials)
506+
return
507+
}
508+
509+
// Present verify email instructions screen. Passing loginFields will prefill the jetpack email in `VerifyEmailViewController`
510+
//
511+
presentVerifyEmail(loginFields: loginFields)
512+
}
513+
482514
// MARK: - Private Constants
483515

484516
/// Rows listed in the order they were created.
@@ -534,40 +566,13 @@ extension SiteCredentialsViewController {
534566
}
535567

536568
func finishedLogin(withUsername username: String, password: String, xmlrpc: String, options: [AnyHashable: Any]) {
537-
guard let delegate = WordPressAuthenticator.shared.delegate else {
538-
fatalError("Error: Where did the delegate go?")
539-
}
540-
541569
let wporg = WordPressOrgCredentials(username: username, password: password, xmlrpc: xmlrpc, options: options)
542-
let credentials = AuthenticatorCredentials(wporg: wporg)
543-
544570
/// If `completionHandler` is available, return early with the credentials.
545571
if let completionHandler = completionHandler {
546-
return completionHandler(wporg)
547-
}
548-
549-
guard configuration.isWPComLoginRequiredForSiteCredentialsLogin else {
550-
// Client didn't explicitly ask for WPCOM credentials. (`isWPComLoginRequiredForSiteCredentialsLogin` is false)
551-
// So, sync the available credentials and finish sign in.
552-
//
553-
delegate.sync(credentials: credentials) { [weak self] in
554-
NotificationCenter.default.post(name: Foundation.Notification.Name(rawValue: WordPressAuthenticator.WPSigninDidFinishNotification), object: nil)
555-
self?.showLoginEpilogue(for: credentials)
556-
}
557-
return
558-
}
559-
560-
// Try to get the jetpack email from XML-RPC response dictionary.
561-
//
562-
guard let loginFields = makeLoginFieldsUsing(xmlrpc: xmlrpc, options: options) else {
563-
WPAuthenticatorLogError("Unexpected response from .org site credentials sign in using XMLRPC.")
564-
showLoginEpilogue(for: credentials)
565-
return
572+
completionHandler(wporg)
573+
} else {
574+
syncDataOrPresentWPComLogin(with: wporg)
566575
}
567-
568-
// Present verify email instructions screen. Passing loginFields will prefill the jetpack email in `VerifyEmailViewController`
569-
//
570-
presentVerifyEmail(loginFields: loginFields)
571576
}
572577

573578
override func displayRemoteError(_ error: Error) {

0 commit comments

Comments
 (0)