We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Oiy... I guess I’m missing that crufty code. I have some Snippets CMPs etc that are using userLoggedIn, getLoginUserID, and others... I don’t even see a good page in the docs that list some of these object methods or their replacements.

    Anyone have alternatives worked out?

    Thanks!
      • 28215
      • 4,149 Posts
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 3749
        • 24,544 Posts
        From the book (which is getting *very* close to being ready to print):

        Is the User Logged In?
        We could check the user’s ID or username with one of the methods above, but there is a more efficient way — calling the user object’s hasSessionContext() method. As we mentioned, for any page request, MODX creates a $user object.

        The user object’s hasSessionContext() method will return true if the user is logged in (authenticated) and false if the user is not logged in.

        If you are only using the default web context (and if you don’t know, then you are), the following code will tell you if the current user is logged in to the front end:
        $isAuthenticated = $modx‑>user‑>hasSessionContext('web');


        If you are using multiple contexts and want to know if the user is logged in to the current context, you can do this:
        $isAuthenticated = $modx‑>user‑>hasSessionContext(
        $modx‑>context‑>get('key'));

        If you are using multiple contexts and want to know if the user is logged in to any of them, this code will do it:
        $isAuthenticated = $modx‑>user‑>hasSessionContext(array(
        'context1',
        'context2',
        'context3'),
        );

        If you are previewing the site from the MODX Manager, the methods above will still work as described. You will not be authenticated unless you log in using a login form in the front end. Remember, though, that you are still logged in to the Manager, so you may still see unpublished documents and can have manager permissions that a normal front‑end user doesn’t have. To see the site as regular users see it, it’s best to preview the site in another browser where you are not logged in to the Manager.

        Note:
        The Login package provides a snippet called isLoggedIn that will check the user’s login status for you. If the user is not logged in to the current context, it will automatically redirect the user to the unauthorized page or to a page you specify in the snippet properties.
          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
        • You’re a gem Bob. Ok, that’s got me out of the fire (thank you)... now I’m tinkering with how to get the user_id. I’m tracking the user_id as the user submits various forms.... I’m still looking for the alternative to getLoginUserID ...
            • 28215
            • 4,149 Posts
            $modx->user->get(’id’);
              shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com