We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13736
    • 345 Posts
    How do I go about redirecting a user to the Login page if they try to access a page that tries to display user info. For example a page with the user Profile information.

    I sort of remember there being a snippet in Evo that would redirect a user to the login page if they weren’t logged in. Is there something in the Login suite that can do that?
      • 28215
      • 4,149 Posts
      No, but that’d be a nice assistance snippet to package in. Can you file that here: http://github.com/splittingred/Login/issues

      The snippet would basically be this:

      if (!$modx->user->hasSessionContext($modx->context->get('key')) {
          $modx->sendUnauthorizedPage();
      }
      return '';
      
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 22969
        • 158 Posts
        Has anyone any ideas on how to implement this?

        I have several problems just trying to get a basic setup with the login snippet!

        Cheers
        Brett
          • 3749
          • 24,544 Posts
          Put this snippet tag at the top of the <body> section of your template.
          Change ## to the ID of your login page.

          [[!LoginRedirect? &loginPage=`##`]]


          Then create a snippet called LoginRedirect with this code (untested):
          <?php
          /* LoginRedirect Snippet */
          if (!$modx->user->hasSessionContext($modx->context->get('key')) {
              $modx->sendRedirect($modx->makeUrl($loginPage));
          }
          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
            • 22969
            • 158 Posts
            Thanks Bob,

            I will test now.......


            Brett
              • 22969
              • 158 Posts
              Hmmm Just takes me to an error page :-(

              brett
                • 13736
                • 345 Posts
                Apparently this feature was added into the Login package with v1.3.2.

                  • 22969
                  • 158 Posts
                  Yes I read that but im unable to work out how to make use of it smiley

                  Brett
                    • 3749
                    • 24,544 Posts
                    The snippet I posted is not tied to the Login package and should work in any version of Revolution or Login as long as the login page is published and not protected by permissions (just make sure it’s not in any resource groups). You also need to make sure that my snippet is not on the login page (either by using a different template for the login page or rewriting the snippet to return an empty string if the current page is the login page).

                    Shaun’s version will redirect the user to the unauthorized page (as defined in the System Settings). If you set the unauthorized_page System Setting to your login page, it will redirect the user to the login page (as long as both pages are published and not protected by permissions). Again, you want to make sure his snippet is not on the unauthorized page.

                      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
                      • 13736
                      • 345 Posts
                      I finally got around to testing this on the site that inspired my original post, and I can’t get it to work either.

                      But, I got to wondering, how is that unauthorized_page key in System Settings supposed to work in the first place? Why would we need a isLoggedIn snippet. So I’m wondering if I have something setup wrong or if MODx has another bug because with or without the isLoggedIn snippet I only get the 404 error message when trying to access a page that is protected.

                      That is sort of logical, the page url doesn’t exist to a user that isn’t logged in, so I’m assuming the isLoggedIn snippet doesn’t even make it to the processing stage to initiate the redirect. However, the unauthorized_page key doesn’t seem to be doing it’s job either.