@@ -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,38 +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-
544- guard configuration. isWPComLoginRequiredForSiteCredentialsLogin else {
545- // Client didn't explicitly ask for WPCOM credentials. (`isWPComLoginRequiredForSiteCredentialsLogin` is false)
546- // So, sync the available credentials and finish sign in.
547- //
548- delegate. sync ( credentials: credentials) { [ weak self] in
549- NotificationCenter . default. post ( name: Foundation . Notification. Name ( rawValue: WordPressAuthenticator . WPSigninDidFinishNotification) , object: nil )
550- self ? . showLoginEpilogue ( for: credentials)
551- }
552- return
553- }
554-
555- // Try to get the jetpack email from XML-RPC response dictionary.
556- //
557- guard let loginFields = makeLoginFieldsUsing ( xmlrpc: xmlrpc, options: options) else {
558- if let completionHandler = completionHandler {
559- return completionHandler ( wporg)
560- }
561- WPAuthenticatorLogError ( " Unexpected response from .org site credentials sign in using XMLRPC. " )
562- showLoginEpilogue ( for: credentials)
563- return
570+ /// If `completionHandler` is available, return early with the credentials.
571+ if let completionHandler = completionHandler {
572+ completionHandler ( wporg)
573+ } else {
574+ syncDataOrPresentWPComLogin ( with: wporg)
564575 }
565-
566- // Present verify email instructions screen. Passing loginFields will prefill the jetpack email in `VerifyEmailViewController`
567- //
568- presentVerifyEmail ( loginFields: loginFields)
569576 }
570577
571578 override func displayRemoteError( _ error: Error ) {
0 commit comments