How to add URL exclusion logic to Memberstack redirect script to skip signup and homepage? Answered

Post author
Alex Siale

Hey Everyone – I am building some custom redirect workflows based on the MS [Code] tutorial on redirecting users to the same page they currently logged in or signed up on.

Works great, but it fires on every page on the website. So, my question is how can I customise this to EXCLUDE a list of URLs? These URLs are pages like ‘the sign up page’, ‘the homepage’. Because that doesn’t make sense to get redirected to a sign up page if you’ve just signend up.

Anyone wannna check my code? Have got somethign working with ChatGPT but always nice for a human to check over

Comments

3 comments

  • Comment author
    Ben Sabic

    Hey Alex Siale!

    I simplified your code a little bit.

    Because you are redirecting the user to the same place (the dashboard), you don't need to include it in your excludedURLs array. Instead, you can just have it directly in your if statement. I also removed https://townhouses.nz from your excluded URLs array, as it is redundant. Visitors will always be redirected to the www subdomain, so it won't be possible for them to ever be on the root domain.

    <script>
      MemberStack.onReady.then(function(member) {
        // Check if the user is already logged in
        if (!member.loggedIn) {
          // Listen for the member.login event to trigger the redirect after login
          member.on('login', function() {
            runRedirectScript();
          });
          // Listen for the member.signup event to trigger the redirect after signup
          member.on('signup', function() {
            runRedirectScript();
          });
        }
      });
    
      function runRedirectScript() {
        // Define an array of excluded URLs
        const excludedURLs = [
          'https://www.townhouses.nz',
          'https://www.townhouses.nz/new-user-signup',
          'https://www.townhouses.nz/signup'
        ];
    
        // Check if the current URL is in the excluded URLs array
        const currentURL = location.href;
    const isExcluded = excludedURLs.some(url => currentURL.includes(url));
    
        if (!isExcluded) {
          // Save the URL of the page you're on for redirecting back to it
          localStorage.setItem('memberstack-location', currentURL);
        } else {
          // Redirect to the account dashboard
          window.location.href = '/account/dashboard';
        }
      }
    </script>
    0
  • Comment author
    Julian Galluzzo

    Hey Alex Siale!

    Check this out, released on Friday 😊

    https://webflow.com/made-in-webflow/website/redirect-to-attempted-page

    It's similar to the one you're using, but it has an exclude list (and it also works when people are attempting to access gated content and then redirected)

    0
  • Comment author
    Alex Siale

    Wow!

    You made the first version I was using.

    Julian Galluzzo you’re a genius!! thanks mate

    success page code:

    <script>
      // Define an array of excluded URLs
      var excludedURLs = [
        ‘https://townhouses.nz’,
        ‘https://www.townhouses.nz’,
        ‘https://www.townhouses.nz/new-user-signup’,
        ‘https://www.townhouses.nz/signup’
      ];


      // Check if the localStorage item exists
      var storedURL = localStorage.getItem(‘locat’);
      if (storedURL) {
        if (excludedURLs.includes(storedURL)) {
          // Redirect to a specific URL for excluded URLs
          window.location = ‘https://www.townhouses.nz/account/dashboard’;
        } else {
          // Redirect back to the stored URL
          window.location = storedURL;
        }
      } else {
        // If localStorage item doesn’t exist, redirect to a default URL
        window.location = ‘https://www.townhouses.nz/account/dashboard’;
      }
    </script>




    Site Code:
    <!-- Include the MemberStack library -->
    <script src=“https://api.memberstack.io/static/memberstack.js”></script>
    <!-- After the MemberStack library is loaded, initialize it -->
    <script>
      MemberStack.onReady.then(function(member) {
        // Check if the user is already logged in
        if (!member.loggedIn) {
          // Listen for the member.login event to trigger the redirect after login
          member.on(‘login’, function() {
            runRedirectScript();
          });

          // Listen for the member.signup event to trigger the redirect after signup
          member.on(‘signup’, function() {
            runRedirectScript();
          });
        }
      });

      function runRedirectScript() {
        // Define an array of excluded URLs and their corresponding redirect URLs
        var excludedURLs = [
          {
            url: ‘https://townhouses.nz’,
            redirect: ‘https://www.townhouses.nz/account/dashboard’
          },
          {
            url: ‘https://www.townhouses.nz’,
            redirect: ‘https://www.townhouses.nz/account/dashboard’
          },
          {
            url: ‘https://www.townhouses.nz/new-user-signup’,
            redirect: ‘https://www.townhouses.nz/account/dashboard’
          },
          {
            url: ‘https://www.townhouses.nz/signup’,
            redirect: ‘https://www.townhouses.nz/account/dashboard’
          }
        ];
        // Check if the current URL is in the excluded URLs array
        var currentURL = location.href;
        var excludedItem = excludedURLs.find(function(item) {
          return currentURL.indexOf(item.url) !== -1;
        });

        if (!excludedItem) {
          // Save the URL of the page you’re on for redirecting back to it
          localStorage.setItem(‘locat’, currentURL);
        } else {
          // Redirect to the assigned redirect URL for the excluded page
          window.location = excludedItem.redirect;
        }
      }
    </script>
    0

Please sign in to leave a comment.