Skip to main content

[Code] Pre-fill Email Fields using Link Parameters

Memberstack Team
Memberstack Team

This feature is super handy when linking from an email to your signup, login, or password reset forms. Since you already know the member's email address it is only kind to pre-fill their email field when they land on the page.  

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

<script type="module">
import * as queryString from "https://cdn.jsdelivr.net/npm/native-querystring@1.1.1/+esm";

function prefillInputsByDataAttribute(dataAttribute) {
  const params = queryString.parse(window.location.search);
  const elements = document.querySelectorAll(`[${dataAttribute}]`);
  elements.forEach(element => {
    if (element.tagName === "INPUT") {
      element.value = params["ms-email"] || "";
    }
  });
}

window.$memberstackDom.getCurrentMember()
.then(({ data: member }) => {
  prefillInputsByDataAttribute("data-ms-member=email");
});
</script>

The link param should look something like this:

www.yourdomain.com/login?ms-email=email@gmail.com.

Was this article helpful?

Didn’t find your answer?

Get an instant answer from Rey, or reach a human. Either way, we’re happy to help.

Comments

10 comments

  • Massak Chrait
    Massak Chrait

    Memberstack Team

    If the snippet doesn't work, add: 

    <script type="module">

    In your opening script :)

    2
  • Duncan from Memberstack
    Duncan from Memberstack

    Thank you Massak Chrait 🙏

    btw - how did you get the code element to appear in your comment? I've been struggling to figure that out! haha 

    0
  • Massak Chrait
    Massak Chrait Edited

    Duncan from Memberstack

    I just clicked on the code block icon in the comment editor... but it seems that it doesn't work anymore somehow 

    0
  • Elias null
    Elias null

    Hey! Could this also work for pre filling both the email and OTP code in the passwordless login forms?

    0
  • Memberstack Team
    Memberstack Team Author

    Hey Elias null 👋

    Can you explain step-by-step (from a members perspective) how you'd like this to work? i.e. 1) enter email and otp 2) change page from signup to login 3) automatically carry over the values. etc. 

    0
  • Elias null
    Elias null

    Hey Memberstack Team.

    I would use this within an email. I could send them a automatically generated link, the email and OTP will be pre filled, and they would instantly be logged in when they click on the e-mail link. Wondering if this is possible. E-mail at least should be possible, but with OTP would be even better, because you could (in theory) use this to create passwordless login.

    I'm curious what you think!

    1
  • Duncan from Memberstack
    Duncan from Memberstack

    Cool idea! Assuming you are programmatically creating/sending the emails then it should be possible. (Meaning you would not be able to use the native Memberstack emails).

    Then you could use a slightly modified version of MemberScript 67 to populate the email from a link param. 

    0
  • Elias null
    Elias null

    Thanks Duncan from Memberstack! Yeah, that was to be expected. Would you be able then to auto generate the OTP on behalf of the recipient? And am I able to choose a timeframe for the expiry? (24h for example?). It seems like this is a cool way to implement passwordless login with Memberstack.

    0
  • Duncan from Memberstack
    Duncan from Memberstack

    Elias null there is no way to configure or modify the OTP 🤔 But if you explain why this is important to you, I can pitch the idea in the next product roadmapping call. 

    0
  • Elias null
    Elias null

    Yeah sure! It would be super helpful if I can send out an email with a button that instantly logs the user in into their account. If, in whatever way, I'm able to make a link that logs them in, that would be cool! I don't know how this would work technically, but I can imagine the E-mail and OTP being prefilled into the login form when they click on the button in my email. This way they instantly log in without switching between the inbox and my app 2 times in order to log in with OTP. Let me know if you have any more questions.

    0

Please sign in to leave a comment.

Sitemap