We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49311
    • 22 Posts
    Currently, I have a login page that redirects the user to a "members only page" that has additional navigation to other sub pages. Only the login page is viewable on the menu.

    Is there a way to detect if the user is already logged in, so that if they click the login screen again, it will automatically redirect them to the "members only page" and not just the login page again with the option to logout?

    Thanks.

    This question has been answered by BobRay. See the first response.

      • 3749
      • 24,544 Posts
      Putting a tag for this snippet in the template or at the top of the page content should do it:

      [[!BypassLogin]]


      /* BypassLogin snippet */
      if ($modx->user->hasSessionContext($modx->context->get('key'))) {
          $memberPageId = 12; // set to actual ID of the member page
          $url = $modx->makUrl($memberPageId, "", "", "full");
          $modx->sendRedirect($url);
      }
      return '';
      


      If the context is always 'web', you can speed it up a little by making it:

      if ($modx->user->hasSessionContext('web')) {



        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
      • There are probably a dozen ways you can do this. One way would be to use the Personalize snippet to load one chunk with the "Login" button for visitors, and another chunk for logged-in users.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 49311
          • 22 Posts
          Thanks BobRay! That did the trick. I did have to edit MakURL to MakeURL.

          I appreciate the help!
            • 49311
            • 22 Posts
            BobRay,

            So I just noticed the redirect causes the logout not to work.

            From the Login documentation, the code for a logout would be

            <p><a href="[[~17? &service=`logout`]]">Logout</a></p>


            The code needs to point the original login page ID for the logout to work, which now has the redirect, so the logout does not work.

            Any thoughts on getting around this?

            Thanks for the help.
              • 3749
              • 24,544 Posts
              I think this would do it (untested):

              /* BypassLogin snippet */
              if ($modx->user->hasSessionContext($modx->context->get('key'))) {
                  if (isset($_GET['service']) && ($_GET['service'] === 'logout')) {
                      return '';
                  }
                  $memberPageId = 12; // set to actual ID of the member page
                  $url = $modx->makUrl($memberPageId, "", "", "full");
                  $modx->sendRedirect($url);
              }
              return '';
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 49311
                • 22 Posts
                Thanks BobRay for the quick reply.

                I just replaced the snippet and it did not work.

                When I click on the Logout link, it stills redirects me to the page "member page".

                Thanks again for the help. It's greatly appreciated.
                • discuss.answer
                  • 3749
                  • 24,544 Posts
                  Try this:

                  /* BypassLogin snippet */
                  if (isset($_GET['service']) && ($_GET['service'] === 'logout')) {
                      return '';
                  }
                  
                  if ($modx->user->hasSessionContext($modx->context->get('key'))) {
                      $memberPageId = 12; // set to actual ID of the member page
                      $url = $modx->makUrl($memberPageId, "", "", "full");
                      $modx->sendRedirect($url);
                  }
                  return '';
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 49311
                    • 22 Posts
                    BobRay,

                    That did the trick.

                    Thanks again for all of your help!
                      • 3749
                      • 24,544 Posts
                      I'm glad I could help. smiley
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting