From ed55a6a36b9250db50e3edaf31b53ce4fc35edfe Mon Sep 17 00:00:00 2001 From: Caleb Pollman Date: Fri, 1 Dec 2023 11:26:25 -0800 Subject: [PATCH] fix(authenticator): exclude empty phone_number values from sign up submit (#4801) --- .changeset/eleven-beers-marry.md | 5 ++++ .../authenticator/__tests__/utils.test.ts | 23 +++++++++++++++---- .../machines/authenticator/actors/signUp.ts | 4 ++-- .../ui/src/machines/authenticator/utils.ts | 3 ++- 4 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 .changeset/eleven-beers-marry.md diff --git a/.changeset/eleven-beers-marry.md b/.changeset/eleven-beers-marry.md new file mode 100644 index 00000000000..5a08a4d48d2 --- /dev/null +++ b/.changeset/eleven-beers-marry.md @@ -0,0 +1,5 @@ +--- +"@aws-amplify/ui": patch +--- + +fix(authenticator): exclude empty phone_number values from sign up submit diff --git a/packages/ui/src/machines/authenticator/__tests__/utils.test.ts b/packages/ui/src/machines/authenticator/__tests__/utils.test.ts index effe18a96fd..233df488cc5 100644 --- a/packages/ui/src/machines/authenticator/__tests__/utils.test.ts +++ b/packages/ui/src/machines/authenticator/__tests__/utils.test.ts @@ -13,12 +13,25 @@ describe('getUserAttributes', () => { it('does not returns the phone_number attribute when undefined', () => { const formValues = { - email: 'example#example.com', + email: 'example@example.com', + country_code: '+1', phone_number: undefined, }; const output = getUserAttributes(formValues); - const expected = { email: 'example#example.com' }; + const expected = { email: 'example@example.com' }; + expect(output).toStrictEqual(expected); + }); + + it('returns an undefined value phone_number attribute when phone_number is an empty string', () => { + const formValues = { + email: 'example@example.com', + country_code: '+1', + phone_number: '', + }; + const output = getUserAttributes(formValues); + + const expected = { email: 'example@example.com' }; expect(output).toStrictEqual(expected); }); }); @@ -31,7 +44,7 @@ describe('getSignUpInput', () => { phone_number: '8002428976', password: 'a_password', confirm_password: 'a_password', - email: 'example#example.com', + email: 'example@example.com', country_code: '+26', }; @@ -41,7 +54,7 @@ describe('getSignUpInput', () => { options: { autoSignIn: true, userAttributes: { - email: 'example#example.com', + email: 'example@example.com', phone_number: '+268002428976', }, }, @@ -58,7 +71,7 @@ describe('getUsernameSignUp', () => { phone_number: '8002428976', password: 'a_password', confirm_password: 'a_password', - email: 'example#example.com', + email: 'example@example.com', country_code: '+26', }; diff --git a/packages/ui/src/machines/authenticator/actors/signUp.ts b/packages/ui/src/machines/authenticator/actors/signUp.ts index 78ae827a3e9..9cc694670e1 100644 --- a/packages/ui/src/machines/authenticator/actors/signUp.ts +++ b/packages/ui/src/machines/authenticator/actors/signUp.ts @@ -293,14 +293,14 @@ export function signUpActor({ services }: SignUpMachineOptions) { async federatedSignIn(_, { data }) { return signInWithRedirect(data); }, - async handleSignUp(context, _event) { + async handleSignUp(context) { const { formValues, loginMechanisms, username } = context; const loginMechanism = loginMechanisms[0]; const input = getSignUpInput(username, formValues, loginMechanism); return services.handleSignUp(input); }, - async validateSignUp(context, _event) { + async validateSignUp(context) { // This needs to exist in the machine to reference new `services` return runValidators( diff --git a/packages/ui/src/machines/authenticator/utils.ts b/packages/ui/src/machines/authenticator/utils.ts index 3f0e7c82a29..36a323c1d8b 100644 --- a/packages/ui/src/machines/authenticator/utils.ts +++ b/packages/ui/src/machines/authenticator/utils.ts @@ -7,6 +7,7 @@ import { isString } from '../../utils'; // default `autoSignIn` flag is `true` const DEFAULT_AUTO_SIGN_IN = true; +const EMPTY_STRING = ''; export const sanitizePhoneNumber = (dialCode: string, phoneNumber: string) => `${dialCode}${phoneNumber}`.replace(/[^A-Z0-9+]/gi, ''); @@ -49,7 +50,7 @@ export const getUserAttributes = ( ); // only include `phone_number` attribute in `userAttributes` if it has a value - if (isString(phone_number)) { + if (isString(phone_number) && phone_number !== EMPTY_STRING) { const { country_code } = formValues; return { ...userAttributes,