We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22656
    • 121 Posts
    Is this possible? I want to allow web users to register, and then automatically be logged in without having to fill in the username/password box again.

    Something like
    $modx->login->loginUser([’webuser_id’])
    which would automagically perform the login process, and allow me to get on with custom script execution.

    i’ve searched the forums without luck, and default login snippet is too complicated to hack apart...
    anything like this exists?
    • Probably be easiest to create a plugin using OnWebSaveUser, and "borrow" the login code from weblogin to set the SESSION and other things that get done on login.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 22656
        • 121 Posts
        Excellent, thanks for the pointer! Will attempt to sort this out sometime soon. Im letting users login manually after registration at the moment (not ideal)
          • 14868
          • 17 Posts
          I’m looking for the same thing. I looked through the WebLogin snippet and it seems like there would be a way to use that existing code to login a user with a username and pwd. I’m looking at weblogin.processor.inc.php lines 189-400. That’s a lot of functionality I’d rather not have to duplicate.

          The other login snippet I noticed was "WebLoginPE" but couldn’t find an example of using her existing classes to login with a username/pwd.

          Has anybody figured out how to do this?

          Having a username and pwd (generated from a subscription service) is it possible to force login a webuser using either the existing code of WebLogin or WebLoginPe snippets?

          any help would be appreciated!
            • 36805
            • 354 Posts
            I also needed to log in a user programatically myself. Afaik existing code of neither Weblogin nor WebloginPE can do this.

            The default Weblogin snippet is indeed way too complicated to use as an inspiration. By far the easier way was to go the WebloginPE route.

            I found it needed to be extended to offer this function. So I made a new class which extends WebLoginPE and provides a function called "TriggerLogin".

            How to install: Simply create a file called webloginpe.extended.class.php with the following code in it and store it in assets/snippets/webloginpe:
            require 'webloginpe.class.php';
            
            class WebloginPEextended extends WebloginPE
            {
            	/**
            	 * TriggerLogin
            	 * Perform all the necessary functions to establish a secure user session with permissions
            	 * Similar to Login() but works manually
            	 * (instead of processing username/password from $_POST it takes username/password arguments) 
            	 *	 
            	 * @param string $username
            	 * @param string $password
            	 * @return void
            	 * @author Johan "MrDutchy" van den Broek
            	 */
            	function TriggerLogin($username, $password, $customTable = 'web_user_attributes_extended')
            	{
            		global $modx;
            		$this->CustomTable($customTable,'');
            		$this->Username = $modx->db->escape(strip_tags($username));
            		$this->Password = $modx->db->escape(strip_tags($password));
            		if ($this->Username == '' || $this->Password == '')
            			return;			
            		$this->OnBeforeWebLogin();
            		$this->User = $this->QueryDbForUser($this->Username);
            		if ($this->User == false)
            			return;
            		$this->UserIsBlocked();
            		$this->Authenticate();
            		$this->SessionHandler('start');
            		$this->OnWebLogin();
            		$this->ActiveUsers();
            		$this->UserDocumentGroups();
            	}
            }

            To demonstrate this works I wrote a simple snippet which has a button to trigger a login. For simplicity’s sake I use hardcoded username/password. If these exist in your install it will authenticate by them. This can easily be integrated into plugins or snippets in any cases you need it.
            include_once MODX_BASE_PATH.'assets/snippets/webloginpe/lang/de.php';
            include_once MODX_BASE_PATH.'assets/snippets/webloginpe/webloginpe.extended.class.php';
            
            if (count($_POST) > 0 && isset($_POST['doDummy']))
            {
              $wlpe = new WebLoginPE($wlpe_lang);
              return $wlpe->TriggerLogin('mywebuser', 'mypassword');
            }
            else
            {
              return
            '<form action="" method="post">
            <input type="submit" name="doDummy" value="doDummy" />
            </form>';
            }
              • 20046
              • 14 Posts
              Looking for similar solution for MODX Revo. Has anyone figured out how to do this with Login snippet? Currently i have working Login registration form, but my client demands that registered users must be logged in automatically after submitting the form.
                • 12893
                • 13 Posts
                I’m also looking for a solution, any help would be appreciated. Thanks!
                  • 4058
                  • 17 Posts
                  MrDutchy, I tried to use your code here, but I wasn’t able to make it work correctly. I get undefined method errors. Could you provide a step by step, for the less PHP savvy of us.

                  Thanks grin
                    • 36805
                    • 354 Posts
                    Quote from: brettdusek at Jul 05, 2011, 11:04 AM

                    I get undefined method errors.
                    Make sure you are using MODx Evolution and WebloginPE is installed which it isn’t by default. Get it from here
                      • 16348
                      • 64 Posts
                      This solution is valid for Revo vith Login snippet (Register).

                      I’m using this snippet as a postHook for Register and it works just fine for me. The code is borrowed from Login.

                      $user = $hook->getValue('register.user');
                      if ($user == null) { return false; }
                        
                      $modx->user =& $user;
                      $modx->getUser();
                      
                      $contexts = !empty($scriptProperties['authenticateContexts']) ? $scriptProperties['authenticateContexts'] : $modx->context->get('key');
                      $contexts = explode(',',$contexts);
                        foreach ($contexts as $ctx) {
                        $user->addSessionContext($ctx);
                      }
                      
                      return true;