How to restrict access to login and signup pages for logged-in members? Answered

Hi, is it possible to only allow access to certain pages for members that are not logged in? I would like /login and /signup to not be accessible when someone is logged in

Comments

6 comments

  • Comment author
    William de Broucker

    you can do it with a bit of custom code

    <script>
    const memberstack = window.$memberstackDom
    memberstack.getCurrentMember().then((member) => {
          if (member.data) {
               window.location.replace("/");
          }
        })
    </script>
    I use it for the login page
    0
  • Comment author
    Chukwudi

    Awesome! You're right, William de Broucker 👏
    To further tailor it down to your request Cameron Bensimon, I added the condition for the /login and /signup.

    <script>
    window.$memberstackDom.getCurrentMember().then(({ data: member }) => {
      if (member) {
         if (location.pathname === '/login' || location.pathname === '/signup' ) {
      window.location.replace('/'); // replace the current URL with the root URL
    }
      } 
    })
    </script>
    0
  • Comment author
    Cameron Bensimon

    Thanks guys! William de Broucker Chukwudi

    0
  • Comment author
    Hannah Saxton

    Hi everyone,

    I know this thread is old but I need some new help with it!

    I am using this because of the following:

    When someone pays for my plan, while they are logged in the signup page is prepopulated with their information, but it does not prevent them from checking out. The result is that there are no required fields and unlimited access to the Stripe checkout page to keep paying us without receiving anything.

    So, this is allowing me to prevent logged in people from accessing a dysfunctional checkout page.

    There is still one problem: if someone abandons their cart, they are now logged in. This means they cannot access the cart without logging out. When they do log out, they will not be able to use their logged in email address. I want abandoned carts to be able to purcase at a later time!

    Can code be added to this so that only people logged in who have access to my paid plan are barred from the signup page?

    0
  • Comment author
    Karolis Vaiginis

    is there someone to help? I want to make logged in members to access only certain URL's and if they are not logged in, to not let them access those URL's. How do I do this?

    0
  • Comment author
    Chukwudi

    Hi Karolis,

    You can achieve that using our gated content feature.

    Alternatively, you can add the Javascript code below before the </head> section of the restricted pages.

    <script> 
    window.addEventListener('DOMContentLoaded', () => {
    window.$memberstackDom.getCurrentMember().then(async ({ data: member }) => {
    if (!member) {
    // If the user is not logged in, redirect to the login page
    window.location.href = '/login'; // Replace '/login' with your actual login page URL
    }
    });
    });
    </script>
    0

Please sign in to leave a comment.