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

3 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 Onyekwere

    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 Onyekwere

    0

Please sign in to leave a comment.