Redirect logged in members to a different page Answered

Post author
Ben Tossell

I’ve just set up a ‘proper’ homepage which is just for new visitors. How can I redirect members to a different page if they’re logged in? I have the redirect setup so when they login they go to the right page, but I want it to be if i’m already logged in and land on the homepage i get redirected. I asked the AI bot and got this script but it doesnt seem to be working:

<script> window.$memberstackDom.onReady.then(function() { window.$memberstackDom.getCurrentMember().then(({ data: member }) => { if (member) { // If a user is logged in, redirect them to the specified page window.location.href = '/your-redirect-page'; // Change '/your-redirect-page' to the path of your desired page } }); }); </script>

I also found the below script from this link https://docs.memberstack.com/hc/en-us/articles/13156905423899--Code-Redirect-Logged-in-Members

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

but that doesnt seem to work either..

Comments

6 comments

  • Comment author
    A J

    Ben Tossell, ideally the second script you shared should work. May I know in which page you have pasted this script and what's the page you want the logged-in users to redirect to?

    0
  • Comment author
    Ben Tossell

    i’ve put it before the body tag on our home page in webflow (bensbites.com) and I want to re-direct users to bensbites.com/catalog

    my code looks like this:

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

    unless i need to put the full url instead ?

    0
  • Comment author
    A J

    Oh, that shouldn't be an issue 🤔
    So currently if you are logged in to your site, and access the homepage you are redirected to nowhere?

    Ben Tossell, can you try using this code in your homepage?

    Since you have set the login redirects to /catalog via Memberstack dashboard, the following code should also work. This does the same thing as the previous code, its just another way of doing it.

    <script> window.$memberstackDom.getCurrentMember().then(({ data: member }) => { if (member && member.loginRedirect) { let redirectPath = member.loginRedirect; let currentPath = window.location.pathname; if (currentPath !== redirectPath) { window.location.href = redirectPath; } } }); </script>
    0
  • Comment author
    Ben Tossell

    yeh currently not redirected anywhere

    ok i’ll try that, so change redirectpath to “/catalog” ? with quotes?

    0
  • Comment author
    A J

    No, you don't need to specify the '/catalog' here. Memberstack will make use of the Login redirect you have set up in the dashboard directly. let me know if it works.

    0
  • Comment author
    Ben Tossell

    ah yes it does! great thanks - takes a couple seconds but works 🙂

    appreciate it 🙂

    0

Please sign in to leave a comment.