How to restrict an entire site to logged-in users with only /login as public using Memberstack?

Post author
Stanislav Borysiuk

Hi,
I'd like to implement the following behavior on my site:

  • All pages should be restricted to logged-in users only
  • The only public page should be /login
  • After logging in, the user should have full access to the entire site
  • After logging out, the site should become restricted again and redirect the user to /login

Is there an official or recommended way to achieve this using Memberstack only (without custom scripts)?
If a script is required, could you please share the recommended way to implement this using Gated Content settings and Plan Redirects?

In my case, there's no need to have a public-facing homepage because the marketing site already exists separately. This site is a dedicated portal for existing clients only. All users are expected to be logged in to access any part of the site except the /login page.

Issue: When I try to add the homepage (/) to the Gated content settings, it disappears from the list after saving.
Goal: I want to restrict access to the entire site, including the homepage (/), and redirect non-logged-in users to /login.
Is there a way to manually or otherwise include the / route in the Gated content list so that it remains there after saving? Or is a script-based solution the only option?

Comments

5 comments

  • Comment author
    A J

    Hey Stanislav Borysiuk, you can set the Gated content settings to restrict access to all the pages / folders of your site via Memberstack dashboard (i.e. set the restriction for all pages of your site as you normally would to gate content) and only allow them for members. You can set the access denied URL to be the /login page.

    It seems you can't input / in the gated content settings since that's not the recommended approach and it expects some URL to gate. I am guessing the script might be the solution for now.

    You can gate the other pages simply via gated content settings. For the homepage, you can probably have a simple script in the before </body> tag section of custom code, which checks if the member is logged in, if yes, leave it as it is, if not send them to /login.
     
    If you want to ensure that even if the page loads for a split second, the content on the homepage should not be visible then, you can additionally add the data-ms-content attribute accordingly to ensure that the elements are only displayed after the check is done that the members are logged in. Here's a detailed guide on that as well. Hope this gives you some idea.
    0
  • Comment author
    Stanislav Borysiuk

    Thanks for the clarification!

    Since the / route can't be gated via the dashboard, could you please share a recommended or official script that checks if the user is logged in and redirects them to /login if not?
    It would be helpful to have an example that aligns with Memberstack’s best practices

    0
  • Comment author
    A J

    Stanislav Borysiuk, oh wow, I had tested this out for your use-case.

    Here's a simple script which should do the trick for you, I tested it out with a personal dummy site:

    <script>
      const accessDeniedPath = "/login";  
      window.$memberstackDom.getCurrentMember().then(({ data: member }) => {
        if (!member) {      
            window.location.href = accessDeniedPath;
        }
      });
    </script>
    

    If you see the docs, it share a similar code for securing dashboard pages, so the above code should work just fine. Additionally, you can use the gated content attributes to ensure the content does not show up until Memberstack has checked the user is logged in.

    Place this script only on the homepage's before the closing </body> tag custom code section, the rest of the pages you can set the gating rules directly via memberstack dashboard.

    0
  • Comment author
    Stanislav Borysiuk

    Hello,

    Could you please explain why, after logging in, the user is not redirected to the homepage (/), even though in Memberstack settings I have set the homepage as the post-login redirect page?

    https://datadevportal.webflow.io/login

    0
  • Comment author
    A J

    Stanislav Borysiuk, have you tried adding '#' as the login redirect for that plan? Does that work for your project?

    I tested this with my personal dummy site and it redirected to the homepage after login.

    In case, it still does not work for you, can you share a read-only link of the site to troubleshoot this?

    0

Please sign in to leave a comment.