We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • I was trying to use the Login snippet on a resource coming from a context I created (not the basic ’web’ context). I haven’t had any problems with the context otherwise, but for some reason it doesn’t have access to $modx->user. When I call [[!Login]] from a web-context resource, it works properly. But when I call it from a my-context resource, the page blows up in the browser (is blank) and the error log shows "PHP Fatal error: Call to a member function isAuthenticated() on a non-object in ...". This is coming from line 76 of Login.Login:
    $authenticated = $modx->user->isAuthenticated($loginContext);


    gettype($modx->user) is returning NULL.

    Is there something I forgot to do in my context setup that is preventing it from being able to access $modx->user?

      • 22019
      • 390 Posts
      I am totally guessing here, but have you tried
      $modx->context->set('mycontext')


      (I’ve got no idea off the top of my head if ’set’ is allowed as well as ’get’).

      Or setting the $sessionContexts? (from the API reference http://api.modxcms.com/modx/modUser.html

       array $sessionContexts = array () (line 13)
      
      A collection of contexts which the current principal is authenticated in.
      
          * access: public


        Writer > E-consultant > MODx developer || Salesforce || modx 2.x || PHP 5.2.13 || MySQL client 5.0.86
        • 3749
        • 24,544 Posts
        The recommended way to switch contexts is:

        $modx->switchContext('contextName');


        You may have failed to initialize the alternate context:

        $modx->initialize('contextName');


        It should be done in that context’s index.php file or in the plugin that directs to the context if you’re doing it that way.


          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
        • Quote from: BobRay at Nov 30, 2010, 08:59 PM

          The recommended way to switch contexts is:

          $modx->switchContext('contextName');


          You may have failed to initialize the alternate context:

          $modx->initialize('contextName');


          It should be done in that context’s index.php file or in the plugin that directs to the context if you’re doing it that way.




          I’m setting the context correctly in this context’s index.php file.

          As far as switching contexts, or using user->addSessionContext, I have no idea where or how I’d do that. Esp. since I can’t seem to even access $modx->user from the context. I feel kinda lost here.
            • 3749
            • 24,544 Posts
            Do you have the latest version of the Login snippet?
              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
            • Yes - installed it today.
                • 3749
                • 24,544 Posts
                That may be a bug (assuming that the context initialization is actually occurring). When the context is initialized, I think it’s supposed to create a user object.

                The only thing I can suggest is to put:

                die('About to Initialize Context');
                

                just above this line in the index.php file for the context. It will show you whether that index.php is actually executing to that point:

                $modx->initialize('ContextName');


                  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
                • Is this on the same domain? If these are not on the same domain you will not be able to share sessions, as the session cookies can only be set to a specific domain/subdomain, or to any subdomain of a single domain. But they cannot be shared across domains as that is not allowed by browsers.

                  If this is a subdomain, you can set a system setting, session_cookie_domain, with a value of:
                  .domain.tld

                  The period at the beginning will allow you to share the session cookie across any subdomains you have set-up as Contexts.
                  • It is a subdomain - and also running on https if that makes any difference.

                    But the user is still anonymous at this point - there has been no log in. Are you saying that session-cookie-sharing is necessary for MODx to even access $modx->user from this alternate context and determine that the user is anonymous/not logged in?

                    • No; the $modx->user will be a "transient" object representing an anonymous user if no user is authenticated in the session for that Context (or the mgr Context; if logged into the mgr Context, and not logged into a front-end Context like web, the user will still be anonymous but will inherit policy permissions from their mgr user session (if it is accessible).