We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13643
    • 44 Posts
    I have a script that works with VisionCart to automatically log in a user, similar to http://www.hyper-typer.com/news/auto-login-on-registration-modx-revolution"" target="_blank" rel="nofollow">this one by Joe Molloy. I'm trying to take this to the next level, and speed up checkout even more by taking the user straight to the page where they enter their shipping address, etc., and using their email address, and an auto-generated, hidden password field to log them in. Here is my code:
    $vc =& $modx->visioncart;
    
    if (!$modx->user->isAuthenticated()) {
      $modx->runSnippet('Register',array(
        'submitVar' => 'vc_user_form',
        'activation' => 0,
        'usernameField' => 'email',
        'usergroups' => 2,
        'postHooks' => 'autoLogin',    
      ));
      
    }
    
    $processor = $modx->runProcessor('user/update', $scriptProperties['fields'], array(
    	'location' => 'web',
    	'processors_path' => $vc->config['processorsPath'],
    	'hook' => &$hook
    ));
    
    return $processor->getResponse();
    

    This actually almost works. The auto-login snippet referenced by the post hook on the Register call does in fact log the user in:
    $c = array(
                        'login_context' => 'web',
                        'username' => $_POST['email'],
                        'password' => $_POST['password']
                    );
                    $response = $modx->runProcessor('security/login',$c);
    


    The trouble is that the call to the user update processor fails because, as I discovered with the incomparable xDebug, $modx->user still points to the anonymous user, even after the auto-login script runs. Thus, I get an error. Reload the page, and everything works fine - the user is logged in, and if I fill out the form again and resubmit it, the user record is updated with the form information.

    The question, then, comes down to this: Is there something I can add to the auto-login script that will fully log the user in id-script, before the page is reloaded?
      • 3749
      • 24,544 Posts
      Try this:

      <?php
      $_POST['username'] = $user;
      $_POST['password'] = $pass; 
      $_POST['service'] = 'login';
      $_REQUEST['service'] = 'login';
      $scriptProperties['loginResourceId'] = 499; // set to page you want to send them to
      
      $modx->runSnippet('Login', $scriptProperties);
      
        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
        • 13643
        • 44 Posts
        Thanks for the suggestion, Bob; this is probably the way I'll end up doing it, but I've pretty much concluded that my original goal is unreachable. Apparently the Login snippet is designed to reload the page when it completes, so anything that gets called after it (in this case, the user update script) simply doesn't run at all. I can see why this might be necessary, since changing user accounts in the middle of a script might prove problematic for the parser. In any case, I'm going to start over and rewrite the script to create the user record, then log the user in last thing and redirect to the next order step.
          • 3749
          • 24,544 Posts
          You might try just using this instead of the return statement:

          <?php
          sleep(2);
          $modx->sendRedirect($modx->makeUrl($id, "","", "full"));
          

          The user may be updated by the time you reach the target 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
            • 13643
            • 44 Posts
            I've already more or less moved off in a different direction with this (moving the auto-login to the end of the script) but I'm curious to know whether you think this would solve my original problem, i.e., that of logging the user in without terminating the script. Doesn't $modx->sendRedirect() take effect immediately, before the rest of the script can be executed?
              • 3749
              • 24,544 Posts
              Yes, when sendRedirect() executes, it terminates the script and starts a new request. The code following sendRedirect() is not executed. If you still need to do something, you can do it in a snippet at the top of the target page, using $_SESSION variables to carry any necessary information forward.
                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
                • 13643
                • 44 Posts
                That's what I thought. So to return to my original question, "Is there any way to log in a user in the middle of a script?" it looks like the answer would be "no," at least if the Login snippet is used, since none of the code after the snippet call will be executed.

                As I mentioned before, I've more or less solved my current problem by moving the Login call to the end of the script, but I just wanted to know for future reference if my original approach could have worked.
                  • 3749
                  • 24,544 Posts
                  I'm not really sure. A page reload might be necessary to refresh things, though you could set a $_SESSION variable and forward to the current page and your script could pick up where it left off.
                    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
                    • 13643
                    • 44 Posts
                    An interesting work-around, to be sure, and one which I'll keep in mind for future situations. For this one, I guess I'll stick with putting it at the end of the script, since that is working and seems to be the simplest solution.