Skip to content

Commit ae3cfc0

Browse files
authored
Merge pull request #7986 from woocommerce/fix/7891-signup-username-error
Account creation: handle email-based username error by generating another fallback username
2 parents ec0a41a + 4d1aa6f commit ae3cfc0

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

Networking/Networking/Remote/AccountRemote.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public enum CreateAccountError: Error, Equatable {
233233
self = .invalidEmail
234234
case Constants.invalidPassword:
235235
self = .invalidPassword(message: message)
236-
case Constants.invalidUsername:
236+
case Constants.invalidUsername, Constants.usernameExists:
237237
self = .invalidUsername
238238
default:
239239
self = .unexpected(error: error)
@@ -249,6 +249,7 @@ public enum CreateAccountError: Error, Equatable {
249249
static let emailExists = "email_exists"
250250
static let invalidEmail = "email_invalid"
251251
static let invalidPassword = "password_invalid"
252-
static let invalidUsername = "username_exists"
252+
static let usernameExists = "username_exists"
253+
static let invalidUsername = "username_invalid"
253254
}
254255
}

Yosemite/Yosemite/Stores/AccountCreationStore.swift

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ private extension AccountCreationStore {
4444
func createAccount(email: String, password: String, completion: @escaping (Result<CreateAccountResult, CreateAccountError>) -> Void) {
4545
Task { @MainActor in
4646
// Auto-generates a username based on the email.
47-
let usernameSuggestionsResult = await remote.loadUsernameSuggestions(from: email)
48-
guard case let .success(usernameSuggestions) = usernameSuggestionsResult,
49-
let username = usernameSuggestions.first else {
47+
guard let username = await generateUsername(base: email) else {
5048
return completion(.failure(.invalidUsername))
5149
}
5250
// Creates a WPCOM account.
@@ -55,7 +53,41 @@ private extension AccountCreationStore {
5553
password: password,
5654
clientID: dotcomClientID,
5755
clientSecret: dotcomClientSecret)
58-
completion(result)
56+
switch result {
57+
case .failure(let error) where error == .invalidUsername:
58+
// Because the username is automatically generated based on the email,
59+
// when there is an error on the username (e.g. when the username contains certain
60+
// keywords like `wordpress`) we want to auto-generate another username using a
61+
// known base so that the user is not blocked on the internal bug where
62+
// `remote.loadUsernameSuggestions` returns an invalid username.
63+
guard let fallbackUsername = await generateUsername(base: Constants.fallbackUsernameBase) else {
64+
return completion(.failure(.invalidUsername))
65+
}
66+
// Creates a WPCOM account with the fallback username.
67+
let result = await remote.createAccount(email: email,
68+
username: fallbackUsername,
69+
password: password,
70+
clientID: dotcomClientID,
71+
clientSecret: dotcomClientSecret)
72+
completion(result)
73+
default:
74+
completion(result)
75+
}
76+
}
77+
}
78+
79+
func generateUsername(base: String) async -> String? {
80+
let usernameSuggestionsResult = await remote.loadUsernameSuggestions(from: base)
81+
guard case let .success(usernameSuggestions) = usernameSuggestionsResult,
82+
let username = usernameSuggestions.first else {
83+
return nil
5984
}
85+
return username
86+
}
87+
}
88+
89+
private extension AccountCreationStore {
90+
enum Constants {
91+
static let fallbackUsernameBase = "woomerchant"
6092
}
6193
}

0 commit comments

Comments
 (0)