@@ -14,10 +14,15 @@ class AppleAuthenticator: NSObject {
1414 // MARK: - Properties
1515
1616 static var sharedInstance : AppleAuthenticator = AppleAuthenticator ( )
17- private override init ( ) { }
1817 private var showFromViewController : UIViewController ?
1918 private let loginFields = LoginFields ( )
2019 weak var delegate : AppleAuthenticatorDelegate ?
20+ let signupService : SocialUserCreating
21+
22+ init ( signupService: SocialUserCreating = SignupService ( ) ) {
23+ self . signupService = signupService
24+ super. init ( )
25+ }
2126
2227 static let credentialRevokedNotification = ASAuthorizationAppleIDProvider . credentialRevokedNotification
2328
@@ -76,59 +81,12 @@ private extension AppleAuthenticator {
7681 return
7782 }
7883
79- tracker. set ( flow: . signupWithApple)
80- tracker. track ( step: . start) {
81- track ( . createAccountInitiated)
82- }
83-
84- 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. " ) )
85-
86- let email = appleCredentials. email ?? " "
87- let name = fullName ( from: appleCredentials. fullName)
88-
89- updateLoginFields ( email: email, fullName: name, token: token)
90-
91- let service = SignupService ( )
92- service. createWPComUserWithApple ( token: token, email: email, fullName: name,
93- success: { [ weak self] accountCreated,
94- existingNonSocialAccount,
95- existing2faAccount,
96- wpcomUsername,
97- wpcomToken in
98- SVProgressHUD . dismiss ( )
99-
100- // Notify host app of successful Apple authentication
101- self ? . authenticationDelegate. userAuthenticatedWithAppleUserID ( appleCredentials. user)
102-
103- guard !existingNonSocialAccount else {
104- self ? . tracker. set ( flow: . loginWithApple)
105-
106- if existing2faAccount {
107- self ? . show2FA ( )
108- return
109- }
110-
111- self ? . updateLoginEmail ( wpcomUsername)
112- self ? . logInInstead ( )
113- return
114- }
115-
116- let wpcom = WordPressComCredentials ( authToken: wpcomToken, isJetpackLogin: false , multifactor: false , siteURL: self ? . loginFields. siteAddress ?? " " )
117- let credentials = AuthenticatorCredentials ( wpcom: wpcom)
118-
119- if accountCreated {
120- self ? . authenticationDelegate. createdWordPressComAccount ( username: wpcomUsername, authToken: wpcomToken)
121- self ? . signupSuccessful ( with: credentials)
122- } else {
123- self ? . authenticationDelegate. sync ( credentials: credentials) {
124- self ? . loginSuccessful ( with: credentials)
125- }
126- }
127-
128- } , failure: { [ weak self] error in
129- SVProgressHUD . dismiss ( )
130- self ? . signupFailed ( with: error)
131- } )
84+ createWordPressComUser (
85+ appleUserId: appleCredentials. user,
86+ email: appleCredentials. email ?? " " ,
87+ name: fullName ( from: appleCredentials. fullName) ,
88+ token: token
89+ )
13290 }
13391
13492 func signupSuccessful( with credentials: AuthenticatorCredentials ) {
@@ -155,18 +113,6 @@ private extension AppleAuthenticator {
155113 showLoginEpilogue ( for: credentials)
156114 }
157115
158- func showSignupEpilogue( for credentials: AuthenticatorCredentials ) {
159- guard let navigationController = showFromViewController? . navigationController else {
160- fatalError ( )
161- }
162-
163- let service = loginFields. meta. appleUser. flatMap {
164- return SocialService . apple ( user: $0)
165- }
166-
167- authenticationDelegate. presentSignupEpilogue ( in: navigationController, for: credentials, service: service)
168- }
169-
170116 func showLoginEpilogue( for credentials: AuthenticatorCredentials ) {
171117 guard let navigationController = showFromViewController? . navigationController else {
172118 fatalError ( )
@@ -220,7 +166,7 @@ private extension AppleAuthenticator {
220166 func updateLoginFields( email: String , fullName: String , token: String ) {
221167 updateLoginEmail ( email)
222168 loginFields. meta. socialServiceIDToken = token
223- loginFields. meta. appleUser = AppleUser ( email: email, fullName: fullName)
169+ loginFields. meta. appleUser = SocialService . User ( email: email, fullName: fullName)
224170 }
225171
226172 func updateLoginEmail( _ email: String ) {
@@ -267,3 +213,80 @@ extension AppleAuthenticator {
267213 ASAuthorizationAppleIDProvider ( ) . getCredentialState ( forUserID: userID, completion: completion)
268214 }
269215}
216+
217+ // This needs to be internal, at this point in time, to allow testing.
218+ //
219+ // Notice that none of this code was previously tested. A small encapsulation breach like this is
220+ // worth the testability we gain from it.
221+ extension AppleAuthenticator {
222+
223+ func showSignupEpilogue( for credentials: AuthenticatorCredentials ) {
224+ guard let navigationController = showFromViewController? . navigationController else {
225+ fatalError ( )
226+ }
227+
228+ let service = loginFields. meta. appleUser. map {
229+ SocialService . apple ( user: $0)
230+ }
231+
232+ authenticationDelegate. presentSignupEpilogue ( in: navigationController, for: credentials, service: service)
233+ }
234+
235+ func createWordPressComUser( appleUserId: String , email: String , name: String , token: String ) {
236+ tracker. set ( flow: . signupWithApple)
237+ tracker. track ( step: . start) {
238+ track ( . createAccountInitiated)
239+ }
240+
241+ SVProgressHUD . show (
242+ withStatus: NSLocalizedString (
243+ " Continuing with Apple " ,
244+ comment: " Shown while logging in with Apple and the app waits for the site creation process to complete. "
245+ )
246+ )
247+
248+ updateLoginFields ( email: email, fullName: name, token: token)
249+
250+ signupService. createWPComUserWithApple (
251+ token: token,
252+ email: email,
253+ fullName: name,
254+ success: { [ weak self] accountCreated, existingNonSocialAccount, existing2faAccount, wpcomUsername, wpcomToken in
255+ SVProgressHUD . dismiss ( )
256+
257+ // Notify host app of successful Apple authentication
258+ self ? . authenticationDelegate. userAuthenticatedWithAppleUserID ( appleUserId)
259+
260+ guard !existingNonSocialAccount else {
261+ self ? . tracker. set ( flow: . loginWithApple)
262+
263+ if existing2faAccount {
264+ self ? . show2FA ( )
265+ return
266+ }
267+
268+ self ? . updateLoginEmail ( wpcomUsername)
269+ self ? . logInInstead ( )
270+ return
271+ }
272+
273+ let wpcom = WordPressComCredentials ( authToken: wpcomToken, isJetpackLogin: false , multifactor: false , siteURL: self ? . loginFields. siteAddress ?? " " )
274+ let credentials = AuthenticatorCredentials ( wpcom: wpcom)
275+
276+ if accountCreated {
277+ self ? . authenticationDelegate. createdWordPressComAccount ( username: wpcomUsername, authToken: wpcomToken)
278+ self ? . signupSuccessful ( with: credentials)
279+ } else {
280+ self ? . authenticationDelegate. sync ( credentials: credentials) {
281+ self ? . loginSuccessful ( with: credentials)
282+ }
283+ }
284+
285+ } ,
286+ failure: { [ weak self] error in
287+ SVProgressHUD . dismiss ( )
288+ self ? . signupFailed ( with: error)
289+ }
290+ )
291+ }
292+ }
0 commit comments