[Code] Redirect Logged-in Members

Article author
Duncan Hamra

This article will show you how to redirect users away from pages they don't need anymore. Usually this is a signup or login page.

Add the following script to the Header or Footer of any page where you want to redirect logged-in members. The script will redirect more quickly if you place it in the header. 

You'll want to replace "/" with something else. Usually a dashboard page or profile page like "/dashboard".

<script>
const memberstack = window.$memberstackDom
memberstack.getCurrentMember().then((member) => {
      if (member.data) {
           window.location.replace("/");
      }
    })
</script>

Was this article helpful?

Comments

5 comments

  • Comment author
    Jean-Léonard Kayaleh

    Hey Duncan Hamra,

    Hope you're doing great.

    I succeeded to achieve what I requested you a few time ago... I share it here in case someone else would like to do the same.

    <script>
    const memberstack = window.$memberstackDom
    memberstack.getCurrentMember().then(({ data: member }) => {
          if (member) {
                // Use member.loginRedirect to obtain the user-specific redirection URL
                const redirectUrl = member.loginRedirect || "/dashboard"; // Fallback to "/dashboard" if no loginRedirect is defined
                window.location.replace(redirectUrl);
          }
    })
    </script>

    Hope this helps 😊

    Thank you again and all the best,
    Jean

    1
  • Comment author
    Duncan Hamra

    Jean-Léonard Kayaleh Can you say more about your script? I'm not 100% sure I understand what you want to achieve. But I'm happy to help if I can!

    0
  • Comment author
    Jean-Léonard Kayaleh

    Thank you !
    Would there be a trick to put the "login-redirect" link inside of this script instead of a static link ?

    0
  • Comment author
    Jean-Léonard Kayaleh

    Thank you very much for your answer Duncan Hamra.

    What I meant is that the script you're proposing requires specifying the URL or slug where the user should land if they are logged in and they arrive on the login or signup page, for example.

    In my case, I would need this URL to be that of my collection page, which uses a dynamic URL I've placed in the 'login-redirect' field of my user in Memberstack.

    So, my question is whether it's possible to have something like this:

    • Instead of: window.location.replace("/");
    • have: window.location.replace("login-redirect");

    I know that this wouldn't work, but the idea is to be able to use the value entered in the 'login-redirect' field of my user.

    0
  • Comment author
    Keith R. Andrade

    Very helpful!! Thank you ✨

    0

Please sign in to leave a comment.