[Code] Concurrent Logout Redirect

Article author
Memberstack Team

In this article, Julian explains how to redirect members when they get logged out by Memberstack's Disable Concurrent Logins feature. 

Add the following code to your site's global <body> tag. 


<script>
window.onload = function() {
  console.log('window.onload fired');

  setTimeout(function() {
    // Check if the user has the _ms-mid item in local storage
    if (localStorage.getItem('_ms-mid')) {
      console.log('_ms-mid item found in local storage');
      
      // Check if the user does NOT have _ms-mem in their local storage
      if (!localStorage.getItem('_ms-mem')) {
        console.log('_ms-mem not found in local storage');
        
        // Check if the user is not already on the /logged-in-elsewhere page
        if (window.location.pathname !== '/logged-in-elsewhere') {
          console.log('Not on /logged-in-elsewhere page, redirecting...');
          
          // Redirect the user to /logged-in-elsewhere
          window.location.href = '/logged-in-elsewhere';
        } else {
          console.log('Already on /logged-in-elsewhere page');
        }
      } else {
        console.log('_ms-mem found in local storage');
      }
    } else {
      console.log('_ms-mid item not found in local storage');
    }
  }, 500); // Delay the execution of the script by 500 milliseconds
};
</script>

Was this article helpful?

Comments

3 comments

  • Comment author
    William de Broucker

    Hello, 

    I tried it but it doesn't work on my website. 

    Does it work in test mode ? (it is not yet a paid plan)

    Thanks,

     

    0
  • Comment author
    Julian Galluzzo

    William de Broucker it seems like we got this resolved!

    For anyone else reading, it seems like GTM was clearing the _ms-mid cookie automatically on logout.

    Luckily, GTM was not needed on his project, so we were able to remove it and fix the error.

    If you are experiencing this issue and you cannot remove GTM/it does not fix the issue, let me know!

    0
  • Comment author
    William de Broucker

    Hello Julian Galluzzo, 

    Can this feature be limited only to heavy users ? (I could flag them manually with a custom field if needed)

    Thanks 

    0

Please sign in to leave a comment.