@@ -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