We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40735
    • 119 Posts
    Hi guys!
    I need a good way to check whether a user is logged in and deliver appropriate content. I've been looking around and I've seen a number of different use cases discussed but not really a generic overview of how to do it. I've seen several different methods for doing it as well but I've not found any discussion about best practices or preferred methods. I've also not seen any examples that don't require writing custom snippets. Does this functionality not exist out of the box? Can it be done with some kind of conditional tag in the template or do I have to write a snippet?

    Right now my plan is to put classes in my markup to specify what should be shown and when, e.g. loggedIn and notLoggedIn, and then write a snippet that does nothing but deliver one line of CSS to the client: either .loggedIn{display:none;} or notLoggedIn{display:none;}. This seems like a less than ideal solution to me but it should work.

    If I go ahead with the aforementioned plan, which method should I use to do the checking? I've seen the following:

    if($modx->getLoginUserName()){ ...
    
    if ($modx->user->hasSessionContext($modx->context->get('key')) ){ ...
    
    if ($modx->getLoginUserID()) {...


    There may be others as well. Any input will be appreciated.
    Thanks!
      • Frogabog- MODX Websites in Portland Oregon
        "Do yourself a favor and get a copy of "MODX - The Official Guide" by Bob Ray. Read it.
        Having server issues? These guys have MODX Hosting perfected - SkyToaster
        • 40735
        • 119 Posts
        I've actually seen that thread but I don't see how that will help me. It seems like that would just add extra layers of functionality to it but not actually address my issue. Let me give you an example of what I'm trying to do. In my navigation I have a button that pops a modal window. If the user is not logged in I'd like the button text to be "Log In" and if they are I'd like it to be their user name ([ [+username]] via the Profile snippet). I don't understand how user groups will help me with this. Can you explain further?
          • 40735
          • 119 Posts
          Another example of what I'm trying to do is deliver CKEditor to the client if the user is logged in so they can edit pages on the front-end. If they're not logged in I don't want them to see the edit controls (even though they couldn't save their edits anyway) or increase load times by delivering the JavaScript when it's not needed.
          • Every logged in user will be a part of the specified user group, which then sees the chunks you're specifying. Anonymous or users from another user group will not see said chunks. (summed up)

            Look at the Login and Subscribe snippets which will provide you with subscribing functionality and plus a nice login form chunks for both logged in and logged out users ($sbsLoggedInDisplayTpl & $sbsLoggedOutDisplayTpl).

            For your front end editors, look at the Frontpate and NewsPublisher Extras.





              Frogabog- MODX Websites in Portland Oregon
              "Do yourself a favor and get a copy of "MODX - The Official Guide" by Bob Ray. Read it.
              Having server issues? These guys have MODX Hosting perfected - SkyToaster
              • 40735
              • 119 Posts
              I'm not specifying any chunks or user groups though. How would I even specify them? I'd need to write a snippet for that wouldn't I? I'm using the Login snippet to generate my form but I haven't seen anything in the docs for it that mentioned any functionality for checking whether a user is logged in. I don't see how setting up subscriptions would help either. I just need small bits of markup in different places in my template to change based on the user's login state.

              I've checked out Frontpage but it doesn't seem to work exactly the way I want so I'm rolling my own that will fit better with my site.
              • The Subscribe documentation at Bob's Guides will help you understand. You don't have to use any of the other Subscribe features to use the loggedIndisplayTpl and loggedOutDisplayTpl chunks. It would simply provide you with them and the snippet is already written.

                Subscribe doesn't rely on user groups, any logged in or out user will work with these chunks.

                User groups would simply provide you with more functionality and subscriber options.
                  Frogabog- MODX Websites in Portland Oregon
                  "Do yourself a favor and get a copy of "MODX - The Official Guide" by Bob Ray. Read it.
                  Having server issues? These guys have MODX Hosting perfected - SkyToaster
                • I forgot to mention, Login provides for checking for logged in and logged out users, and displays the appropriate login or logout chunks automatically.

                  Been looking through some code on one of my sites and this is what I'm using:

                  [[!Login? &loginTpl=`lgnLoginTpl` &logoutTpl=`lgnLogoutTpl` &errTpl=`lgnErrTpl` &loginResourceId=`22` &logoutResourceId=`25`]]


                    Frogabog- MODX Websites in Portland Oregon
                    "Do yourself a favor and get a copy of "MODX - The Official Guide" by Bob Ray. Read it.
                    Having server issues? These guys have MODX Hosting perfected - SkyToaster
                    • 40735
                    • 119 Posts
                    Sweet! This will work perfectly I think. Thanks!
                      • 40735
                      • 119 Posts
                      Wait, I didn't read this right last night. This is just specifying the template for the login form. I guess I could just stick in any chunk that I wanted and just not include the form though. I don't think that's how it's intended to work but I don't see why it won't. I'm going to try that.