11import Foundation
22import AuthenticationServices
33import WordPressKit
4+ import SVProgressHUD
45
56#if XCODE11
67
@@ -57,19 +58,27 @@ private extension AppleAuthenticator {
5758 @available ( iOS 13 . 0 , * )
5859 func createWordPressComUser( appleCredentials: ASAuthorizationAppleIDCredential ) {
5960 guard let identityToken = appleCredentials. identityToken,
60- let token = String ( data: identityToken, encoding: . utf8) ,
61- let email = appleCredentials. email else {
61+ let token = String ( data: identityToken, encoding: . utf8) else {
6262 DDLogError ( " Apple Authenticator: invalid Apple credentials. " )
6363 return
6464 }
6565
66+ SVProgressHUD . show ( withStatus: NSLocalizedString ( " Continuing with Apple " , comment: " Shown while logging in with Apple and the app waits for the site creation process to complete. " ) )
67+
68+ let email = appleCredentials. email ?? " "
6669 let name = fullName ( from: appleCredentials. fullName)
6770
6871 updateLoginFields ( email: email, fullName: name, token: token)
6972
7073 let service = SignupService ( )
7174 service. createWPComUserWithApple ( token: token, email: email, fullName: name,
72- success: { [ weak self] accountCreated, wpcomUsername, wpcomToken in
75+ success: { [ weak self] accountCreated, existingNonSocialAccount, wpcomUsername, wpcomToken in
76+ SVProgressHUD . dismiss ( )
77+
78+ guard !existingNonSocialAccount else {
79+ self ? . logInInstead ( )
80+ return
81+ }
7382
7483 let wpcom = WordPressComCredentials ( authToken: wpcomToken, isJetpackLogin: false , multifactor: false , siteURL: self ? . loginFields. siteAddress ?? " " )
7584 let credentials = AuthenticatorCredentials ( wpcom: wpcom)
@@ -79,12 +88,15 @@ private extension AppleAuthenticator {
7988 self ? . authenticationDelegate. createdWordPressComAccount ( username: wpcomUsername, authToken: wpcomToken)
8089 self ? . signupSuccessful ( with: credentials)
8190 return
91+ } else {
92+ self ? . authenticationDelegate. createdWordPressComAccount ( username: wpcomUsername, authToken: wpcomToken)
93+ self ? . signinSuccessful ( with: credentials)
94+ return
8295 }
83-
84- // Existing WP Account
85- self ? . logInInstead ( )
8696
8797 } , failure: { [ weak self] error in
98+ SVProgressHUD . dismiss ( )
99+
88100 self ? . signupFailed ( with: error)
89101 } )
90102 }
@@ -95,6 +107,11 @@ private extension AppleAuthenticator {
95107 showSignupEpilogue ( for: credentials)
96108 }
97109
110+ func signinSuccessful( with credentials: AuthenticatorCredentials ) {
111+ // TODO: Tracks events for login
112+ showSigninEpilogue ( for: credentials)
113+ }
114+
98115 func showSignupEpilogue( for credentials: AuthenticatorCredentials ) {
99116 guard let navigationController = showFromViewController? . navigationController else {
100117 fatalError ( )
@@ -107,6 +124,14 @@ private extension AppleAuthenticator {
107124 authenticationDelegate. presentSignupEpilogue ( in: navigationController, for: credentials, service: service)
108125 }
109126
127+ func showSigninEpilogue( for credentials: AuthenticatorCredentials ) {
128+ guard let navigationController = showFromViewController? . navigationController else {
129+ fatalError ( )
130+ }
131+
132+ authenticationDelegate. presentLoginEpilogue ( in: navigationController, for: credentials) { }
133+ }
134+
110135 func signupFailed( with error: Error ) {
111136 WPAnalytics . track ( . signupSocialFailure)
112137 DDLogError ( " Apple Authenticator: signup failed. error: \( error) " )
0 commit comments