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

Commit cb877e1

Browse files
authored
Merge pull request #121 from wordpress-mobile/feature/siwa-wait-indicator
Support existing Apple accounts going right into the epilogue
2 parents f73b006 + 5404276 commit cb877e1

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

WordPressAuthenticator.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "WordPressAuthenticator"
3-
s.version = "1.8.0-beta.10"
3+
s.version = "1.8.0-beta.12"
44
s.summary = "WordPressAuthenticator implements an easy and elegant way to authenticate your WordPress Apps."
55

66
s.description = <<-DESC

WordPressAuthenticator/Services/SignupService.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class SignupService {
5151
func createWPComUserWithApple(token: String,
5252
email: String,
5353
fullName: String?,
54-
success: @escaping (_ newAccount: Bool, _ username: String, _ wpcomToken: String) -> Void,
54+
success: @escaping (_ newAccount: Bool, _ existingNonSocialAccount: Bool, _ username: String, _ wpcomToken: String) -> Void,
5555
failure: @escaping (_ error: Error) -> Void) {
5656
let remote = WordPressComServiceRemote(wordPressComRestApi: anonymousAPI)
5757

@@ -68,8 +68,16 @@ class SignupService {
6868
}
6969

7070
let createdAccount = (response?[ResponseKeys.createdAccount] as? Int ?? 0) == 1
71-
success(createdAccount, username, bearer_token)
71+
success(createdAccount, false, username, bearer_token)
7272
}, failure: { error in
73+
if let error = (error as NSError?) {
74+
let existingNonSocialAccount = (error.userInfo[ErrorKeys.errorCode] as? String ?? "") == ErrorKeys.existingNonSocialUser
75+
if existingNonSocialAccount {
76+
success(false, true, "", "")
77+
return
78+
}
79+
}
80+
7381
failure(error ?? SignupError.unknown)
7482
})
7583
}
@@ -96,6 +104,11 @@ private extension SignupService {
96104
static let username = "username"
97105
static let createdAccount = "created_account"
98106
}
107+
108+
struct ErrorKeys {
109+
static let errorCode = "WordPressComRestApiErrorCodeKey"
110+
static let existingNonSocialUser = "user_exists"
111+
}
99112
}
100113

101114

WordPressAuthenticator/Signin/AppleAuthenticator.swift

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import AuthenticationServices
33
import 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

Comments
 (0)