From a188cb9fb610733362febf6be907f984b0479ef1 Mon Sep 17 00:00:00 2001 From: wlee221 Date: Mon, 1 Feb 2021 14:04:13 -0800 Subject: [PATCH 1/2] Handle non-username alias cases --- .../amplify-ui-components/src/common/Translations.ts | 2 ++ .../components/amplify-sign-up/amplify-sign-up.tsx | 11 +++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/amplify-ui-components/src/common/Translations.ts b/packages/amplify-ui-components/src/common/Translations.ts index 961ca22b3ec..a2a0ceff269 100644 --- a/packages/amplify-ui-components/src/common/Translations.ts +++ b/packages/amplify-ui-components/src/common/Translations.ts @@ -24,6 +24,8 @@ export enum AuthStrings { NEW_PASSWORD_PLACEHOLDER = 'Enter your new password', NO_ACCOUNT_TEXT = 'No account?', USERNAME_REMOVE_WHITESPACE = 'Username cannot contain whitespace', + EMAIL_REMOVE_WHITESPACE = 'Email cannot contain whitespace', + PHONE_REMOVE_WHITESPACE = 'Phone number cannot contain whitespace', PASSWORD_REMOVE_WHITESPACE = 'Password cannot start or end with whitespace', PASSWORD_LABEL = 'Password *', PASSWORD_PLACEHOLDER = 'Enter your password', diff --git a/packages/amplify-ui-components/src/components/amplify-sign-up/amplify-sign-up.tsx b/packages/amplify-ui-components/src/components/amplify-sign-up/amplify-sign-up.tsx index 055b39ea8a5..7a95256d810 100644 --- a/packages/amplify-ui-components/src/components/amplify-sign-up/amplify-sign-up.tsx +++ b/packages/amplify-ui-components/src/components/amplify-sign-up/amplify-sign-up.tsx @@ -126,7 +126,7 @@ export class AmplifySignUp { switch (this.usernameAlias) { case 'email': case 'phone_number': - this.signUpAttributes.username = this.signUpAttributes.attributes[this.usernameAlias]; + this.signUpAttributes.username = this.signUpAttributes.attributes[this.usernameAlias] || ''; break; case 'username': default: @@ -134,7 +134,14 @@ export class AmplifySignUp { } try { if (this.signUpAttributes.username.indexOf(' ') >= 0) { - throw new Error(Translations.USERNAME_REMOVE_WHITESPACE); + switch (this.usernameAlias) { + case 'username': + throw new Error(Translations.USERNAME_REMOVE_WHITESPACE); + case 'email': + throw new Error(Translations.EMAIL_REMOVE_WHITESPACE); + case 'phone_number': + throw new Error(Translations.PHONE_REMOVE_WHITESPACE); + } } if (this.signUpAttributes.password !== this.signUpAttributes.password.trim()) { throw new Error(Translations.PASSWORD_REMOVE_WHITESPACE); From cc36343c2bb3d303e0c6ae31308d1b73d6c22c06 Mon Sep 17 00:00:00 2001 From: wlee221 Date: Mon, 1 Feb 2021 15:04:56 -0800 Subject: [PATCH 2/2] Use empty username check instead --- .../src/common/Translations.ts | 2 -- .../components/amplify-sign-up/amplify-sign-up.tsx | 14 +++++--------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/packages/amplify-ui-components/src/common/Translations.ts b/packages/amplify-ui-components/src/common/Translations.ts index a2a0ceff269..961ca22b3ec 100644 --- a/packages/amplify-ui-components/src/common/Translations.ts +++ b/packages/amplify-ui-components/src/common/Translations.ts @@ -24,8 +24,6 @@ export enum AuthStrings { NEW_PASSWORD_PLACEHOLDER = 'Enter your new password', NO_ACCOUNT_TEXT = 'No account?', USERNAME_REMOVE_WHITESPACE = 'Username cannot contain whitespace', - EMAIL_REMOVE_WHITESPACE = 'Email cannot contain whitespace', - PHONE_REMOVE_WHITESPACE = 'Phone number cannot contain whitespace', PASSWORD_REMOVE_WHITESPACE = 'Password cannot start or end with whitespace', PASSWORD_LABEL = 'Password *', PASSWORD_PLACEHOLDER = 'Enter your password', diff --git a/packages/amplify-ui-components/src/components/amplify-sign-up/amplify-sign-up.tsx b/packages/amplify-ui-components/src/components/amplify-sign-up/amplify-sign-up.tsx index 7a95256d810..963bd293c79 100644 --- a/packages/amplify-ui-components/src/components/amplify-sign-up/amplify-sign-up.tsx +++ b/packages/amplify-ui-components/src/components/amplify-sign-up/amplify-sign-up.tsx @@ -126,22 +126,18 @@ export class AmplifySignUp { switch (this.usernameAlias) { case 'email': case 'phone_number': - this.signUpAttributes.username = this.signUpAttributes.attributes[this.usernameAlias] || ''; + this.signUpAttributes.username = this.signUpAttributes.attributes[this.usernameAlias]; break; case 'username': default: break; } try { + if (!this.signUpAttributes.username) { + throw new Error(Translations.EMPTY_USERNAME); + } if (this.signUpAttributes.username.indexOf(' ') >= 0) { - switch (this.usernameAlias) { - case 'username': - throw new Error(Translations.USERNAME_REMOVE_WHITESPACE); - case 'email': - throw new Error(Translations.EMAIL_REMOVE_WHITESPACE); - case 'phone_number': - throw new Error(Translations.PHONE_REMOVE_WHITESPACE); - } + throw new Error(Translations.USERNAME_REMOVE_WHITESPACE); } if (this.signUpAttributes.password !== this.signUpAttributes.password.trim()) { throw new Error(Translations.PASSWORD_REMOVE_WHITESPACE);