We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34193
    • 330 Posts
    OK I am trying to use the login processor to log a user in to modx from an external script. Looking at debug information the login is successful and if I refresh and run again without logging out the isAuthenticated returns true. Here is my code :-

    $response = $modx->runProcessor('security/login', array(
        'username' => $_REQUEST['username'],
        'password' => $_REQUEST['password'],
        'login_context' => 'web',
        'add_contexts' => 'web',
        'rememberme' => true,
            ));
    
    if (!empty($response) && !$response->isError()) {
        $output['finished'] = true;
        $output['success'] = true;
        if ($modx->user->isAuthenticated() == true ){
            $output['loggedin'] = true;
            $output['userName'] = $modx->getLoginUserName();
        }
    } else {
        if ($response->isError()) {
            $output['error'] = $response->getMessage();
        } else {
            $output['error'] = 'Unkown Error, please try again.';
        }
    }


    Am I missing a step between the login and checking if authenticated.
      • 3749
      • 24,544 Posts
      For code login through the Login snippet, you have to explicitly set BOTH $_POST['service'] *and* $_REQUEST['service'] in order for the login to work. Maybe the processor requires that as well.

      According to reports, this works to log in a user (thanks to jrotering):

      $_POST['username'] = $user;
      $_POST['password'] = $pass; 
      $_POST['service'] = 'login';
      $_REQUEST['service'] = 'login';
      $scriptProperties['loginResourceId'] = 499;
      
      $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
        • 34193
        • 330 Posts
        Hi Bob thanks for the suggestion tried both

        $_POST['service'] = 'login';
        $_REQUEST['service'] = 'login';
        
        $response = $modx->runProcessor('security/login', array(
            'username' => $_REQUEST['username'],
            'password' => $_REQUEST['password'],
            'login_context' => 'web',
            'add_contexts' => 'web',
            'rememberme' => true,
                ));


        and
        $response = $modx->runProcessor('security/login', array(
            'username' => $_REQUEST['username'],
            'password' => $_REQUEST['password'],
            'login_context' => 'web',
            'add_contexts' => 'web',
            'rememberme' => true,
            'service' => 'login'
                ));


        Neither seem to work.
        Having looked through the code I can't see any reference to service in the code.
          • 3749
          • 24,544 Posts
          Are you sure that the username and password are in the $_REQUEST array and that they're correct?
            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
            • 28215
            • 4,149 Posts
            What does $response->getMessage(); print out?
              shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
              • 34193
              • 330 Posts
              Hi Bob yes and I've even tried putting the username and password in as a string instead of using the passed in and nothing.

              splittingred Reply
              $response->getMessage();

              display nothing.

              One thing I have just thought of is that the users are extended from Extending modUser for another aspect of the site could this have something to do with it. All though I can log into manager using them and the other part of the site works fine.

              I have also tried running the script getting it returning false and then running it a second time without any changes and it returns true as expected then if I log into manager flush all sessions and run again it is false and once again on the second run it returns true and returns true again and again until I flush the sessions and once again first run it is false every other true. [ed. note: pedley last edited this post 14 years, 11 months ago.]
                • 34193
                • 330 Posts
                Also to be extra sure I have tried logging in with the incorrect credentials and get the following every time

                The username or password you entered is incorrect. Please check the username, re-type the password, and try again.

                from $response->getMessage()

                Which is as expected.