Skip to content

Commit

Permalink
fix(authenticator): exclude empty phone_number values from sign up su…
Browse files Browse the repository at this point in the history
…bmit (#4801)
  • Loading branch information
calebpollman authored Dec 1, 2023
1 parent cb7025e commit ed55a6a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-beers-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@aws-amplify/ui": patch
---

fix(authenticator): exclude empty phone_number values from sign up submit
23 changes: 18 additions & 5 deletions packages/ui/src/machines/authenticator/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,25 @@ describe('getUserAttributes', () => {

it('does not returns the phone_number attribute when undefined', () => {
const formValues = {
email: 'example#example.com',
email: '[email protected]',
country_code: '+1',
phone_number: undefined,
};
const output = getUserAttributes(formValues);

const expected = { email: 'example#example.com' };
const expected = { email: '[email protected]' };
expect(output).toStrictEqual(expected);
});

it('returns an undefined value phone_number attribute when phone_number is an empty string', () => {
const formValues = {
email: '[email protected]',
country_code: '+1',
phone_number: '',
};
const output = getUserAttributes(formValues);

const expected = { email: '[email protected]' };
expect(output).toStrictEqual(expected);
});
});
Expand All @@ -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',
};

Expand All @@ -41,7 +54,7 @@ describe('getSignUpInput', () => {
options: {
autoSignIn: true,
userAttributes: {
email: 'example#example.com',
email: 'example@example.com',
phone_number: '+268002428976',
},
},
Expand All @@ -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',
};

Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/machines/authenticator/actors/signUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/src/machines/authenticator/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '');
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit ed55a6a

Please sign in to comment.