src/store/models/user.js
/**
		 * User attribute update with a fallback in case phone number throws error on Cognito validation.
		 */
User attribute update with a fallback in case phone number throws error on Cognito validation.
User attribute update with a fallback in case phone number throws error on Cognito validation .
async updateUserAttributes({
			phoneNumber,
			storeName,
			website,
			currency,
			country
		}) {
			const attributes = {
				[userAttributeTypes.LOCALE]: "en-AU",
				[userAttributeTypes.STORE_NAME]: storeName,
				[userAttributeTypes.WEBSITE]: website,
				[userAttributeTypes.CURRENCY]: currency,
				[userAttributeTypes.COUNTRY]: country
			};
			const update = async submittedAttributes => {
				const currentUser = await Auth.currentAuthenticatedUser();
				const result = await Auth.updateUserAttributes(
					currentUser,
					submittedAttributes
				);
				const { attributes: userAttributes } = currentUser;
				const newUser = Object.assign({}, currentUser, {
					attributes: {
						...userAttributes,
						...submittedAttributes
					}
				});
				dispatch.user.set(newUser);
				track(eventTypes.SIGN_UP, newUser.attributes);
				Functions.notify({
					text: "New Account Created!",
					fields: newUser.attributes
				});
				return result;
			};
			try {
				await update({
					[userAttributeTypes.PHONE_NUMBER]: phoneNumber,
					[userAttributeTypes.CUSTOM_PHONE_NUMBER]: phoneNumber,
					...attributes
				});
			} catch (e) {
				handleException(new Error("Invalid phone number in use", phoneNumber));
				if (
					e.message.includes("phone") &&
					e.code === "InvalidParameterException"
				) {
					try {
						const result = await update({
							[userAttributeTypes.CUSTOM_PHONE_NUMBER]: phoneNumber,
							...attributes
						});
						if (result !== "SUCCESS") {
							handleException(
								new Error(
									"Something went wrong updating user attributes",
									result
								)
							);
						}
					} catch (subErr) {
						handleException(subErr);
					}
				} else {
					handleException(e);
				}
			}
		}
async updateUserAttributes ( { phoneNumber , storeName , website , currency , country } ) { const attributes = { [ userAttributeTypes . LOCALE ] : "en-AU" , [ userAttributeTypes . STORE_NAME ] : storeName , [ userAttributeTypes . WEBSITE ] : website , [ userAttributeTypes . CURRENCY ] : currency , [ userAttributeTypes . COUNTRY ] : country } ; const update = async submittedAttributes => { const currentUser = await Auth . currentAuthenticatedUser ( ) ; const result = await Auth . updateUserAttributes ( currentUser , submittedAttributes ) ; const { attributes : userAttributes } = currentUser ; const newUser = Object . assign ( { } , currentUser , { attributes : { ... userAttributes , ... submittedAttributes } } ) ; dispatch . user . set ( newUser ) ; track ( eventTypes . SIGN_UP , newUser . attributes ) ; Functions . notify ( { text : "New Account Created!" , fields : newUser . attributes } ) ; return result ; } ; try { await update ( { [ userAttributeTypes . PHONE_NUMBER ] : phoneNumber , [ userAttributeTypes . CUSTOM_PHONE_NUMBER ] : phoneNumber , ... attributes } ) ; } catch ( e ) { handleException ( new Error ( "Invalid phone number in use" , phoneNumber ) ) ; if ( e . message . includes ( "phone" ) && e . code === "InvalidParameterException" ) { try { const result = await update ( { [ userAttributeTypes . CUSTOM_PHONE_NUMBER ] : phoneNumber , ... attributes } ) ; if ( result !== "SUCCESS" ) { handleException ( new Error ( "Something went wrong updating user attributes" , result ) ) ; } } catch ( subErr ) { handleException ( subErr ) ; } } else { handleException ( e ) ; } } }
User attribute update with a fallback in case phone number throws error on Cognito validation.
User attribute update with a fallback in case phone number throws error on Cognito validation .
// Ping Slack that a new user has been created.