Restricting access to an entire subdomain Answered

Post author
Antoine Charrier

Hello everyone,

First off, a big thank you to the Memberstack team for creating such a fantastic product! 🙏

I'm reaching out to ask if anyone knows how to restrict access to an entire subdomain?

While I've successfully implemented Memberstack on both my main domain and subdomain (and managed to restrict certain elements on both websites), my primary goal is to block non-members from accessing the subdomain entirely and redirecting them to the login page on the main domain.

I guess I might be entering wrong the info in the gated content area... Or is this impossible with basic features? Anyone can help?

Comments

11 comments

  • Comment author
    Chukwudi Onyekwere

    Hi Antoine,

    If you're using the same App ID for the main domain and sub domain, then technically, non-members will not gain access to the either of them if you've gated them.

    The only thing I'll suggest is since you want non-members to be redirected to the main domain, you may have to create an extra page for the access denied URL section of the gated content on Memberstack, you put the link to the page.

    Then on the page, add the following meta tag before the closing section.

    <meta http-equiv="Refresh" content="0; url='https://www.urltofthemaindomain.com'" />

    With this, when a non-member visits the subdomain, they get redirected to this page which instantly takes them to the main domain.

    P.S: You may have to create the same page in the main domain too as it shares the same access denied URL. Also, remember to replace the https://www.urltofthemaindomain.com in the code above with the main domain URL.

    0
  • Comment author
    Antoine Charrier

    Hi Chukwudi, ok I'll try the redirect, thanks for the code. But my main issue lies upstream. Yes I have same app Id on both domains, but how to gate the entire sundomain ? In gated content admin page I have gated Url start with or equals app.mywebsite.com but this is not working. Not sure we can gate a subdomain there or that this is the right way to do it.

    0
  • Comment author
    Chukwudi Onyekwere

    If you've added the subdomain to the application domains in Memberstack, It should work for you. However, if you're still having problems with that even after adding it, please let me know.

    0
  • Comment author
    Duncan Hamra

    Antoine Charrier can you use gated content to gate access to all pages on the domain?

    0
  • Comment author
    Antoine Charrier

    Chukwudi-Onyekwere I c an only add 1 domain there right ? I have mywebsite.com
    I can gate elements on the subdomain, but how to gate the full subdomain ?

    here we need the possibility of adding a sub-domain

    0
  • Comment author
    Chukwudi Onyekwere

    In the Memberstack section in your screenshot, you can only gate pages/directories/page elements of a domain/subdomain, not the entire domain/subdomain as you've stated.

    However, if you want to achieve that, you may have to use a Javascript workaround instead. The code snippet below when added in the head code section of the site settings of your subdomain, redirects anyone who isn't logged in (i.e non-members), to the URL you specified in URLToTheOtherDomainGoesHere

    Here's the code snippet:

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

    Do not forget to replace URLToTheOtherDomainGoesHere with the Redirect URL.

    You may also want to enable cross-domain login too. Quick demo video here.

    0
  • Comment author
    Antoine Charrier

    Thank you! My cross domain login is working. But the redirect still not. I'm doing in vanila js. Would you mind sharing a vanila version of this code if possible please ?

    0
  • Comment author
    Chukwudi Onyekwere

    The code is Vanilla JS actually 😌.

    You can put the code before the closing </body> tags of all the pages in your subdomain and it will work.

    Alternatively, you can put the code snippet (without the opening and closing script tags) in a separate file in your project, then name the file something like myscript.js while in the pages on your subdomain (before the closing </body> tags), you can reference the file thus:

    <script src="folderPathHousingTheScriptIfAnyGoesHere/myscript.js"></script>

    I found a random article that wil help in setting this up here just in case.

    0
  • Comment author
    Antoine Charrier

    Thanks!

    (Ah ok I tough since "DOM" was inside the code it means the package memberstack must be installed in my server.)

    I tried the first method (to place the code before </body> tag instead of before </head> tag but it still does not work :
    (index):86 Uncaught TypeError: Cannot read properties of undefined (reading 'getCurrentMember') at (index):86:32 

    I tried also calling the seperate file as you explained (thanks for the tuto) but does not work neither.

    the problem is the getcurrentmember is not defined so I guess I have an issue calling memberstack functions?

    can look at my scripts before </head> please?
    please not that cross domain login is working. Only the redirect is not
    <script data-memberstack-domain="https://memberstack-client.XXX.com" data-memberstack-app="app_XXX" src="https://static.memberstack.com/scripts/v1/memberstack.js" defer></script> 
    
    <script> function initMemberStack() { const memberstackConfig = { useCookies: true, setCookieOnRootDomain: true }; } </script>
     
    when I wrote XXX is to mask my domain in this conversation (edited) 
     
    By the way, it would also be convenient to input any URL, including subdomains on the redirects page:
     
    0
  • Comment author
    Chukwudi Onyekwere

    Your insight is valuable, and we'll definitely take it into consideration. Kindly DM me your app id and website URL so I can look into the errors you're getting.

    0
  • Comment author
    Antoine Charrier

    Thanks ! 🙂

    0

Please sign in to leave a comment.