[Code] Save Email Between Login, Signup, and Password Reset Forms

Article author
Duncan Hamra
  • Updated

The code below saves the member's email field when navigating between login, sign up, and password reset pages.

It listens for keyup events on the email input element and saves the email input's value to localStorage. On page load, it checks if there is a saved email in localStorage and autofills the email input if there is. The code will only run if the member is logged out.

You'll want to add this code to the Footer or <Body> section of any page with a signup form, login form, or forgot password form. 

<script>
	const setEmail = email = localStorage.setItem("email", email);
	const getEmail = () = localStorage.getItem("email");

	function handleEmailInputs(email) {
		const inputs = document.querySelectorAll(`[data-ms-member=email]`);

		inputs.forEach(input = {
			if (input.tagName === "INPUT") {
				input.value = email || "";
				input.addEventListener("keyup", () = setEmail(input.value));
			}
		});
	}
	// Check if $memberstackDom is defined before using getCurrentMember
	if (window.$memberstackDom) {
		window.$memberstackDom.getCurrentMember().then(({ data: member }) = {
			let email = getEmail();
			member && !email && setEmail(member.email);
			!member && handleEmailInputs(email);
		});
	} else {
		// Handle the case where $memberstackDom is not defined
		const email = getEmail();
		handleEmailInputs(email);
	}
</script>

There’s no additional configuration needed on your part!

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.