We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29606
    • 63 Posts
    Has anybody made a user login/logout (maybe even registration) system working with ModX Revolution on the frontend ?
    I find it hard to understand how to deal with it...
    Thanks !
      • 24719
      • 194 Posts
      i was just about to ask how to do it as well.

      in the moduser class i can see basically how it seems to work. it looks like the following script would more or less work:

      <?php
      
      if($user = $modx->getObject('modUser', array('username' => $_POST['username'], 'password' => md5($_POST['password']))))
      {
        $user->addSessionContext($modx->context->get('key'));
      }
      else
        // send an error message saying log in failed
      


      i can’t see the db schema at the moment though, so this might not work.

      is there anything else that needs to happen? I haven’t tried it, but it looks like it should work...
      • You can see (and use) the entire process in core/model/modx/processors/security/login.php.  There are a few changes coming to this before beta, but basically, all you need to do is POST username, password, and login_context through a form, either to the connector that the manager login page uses (via Ajax), or using your own snippet that calls the processor.
          • 29606
          • 63 Posts
          I’ve been away for some time... but my need for a frontend login form is still here.

          I tried using redman code, and it works, indeed, but it doesn’t seem to be a very clean way to do it.

          I kinda understand the logic of what OpenGeek says, but my tests show that this must be beyond my understanding and/or limited programming skills.

          If someone could post just a very simple, minimal code for a frontend login/logout system, I’m sure I can work from it.
            • 29606
            • 63 Posts
            Ok, I managed to do it, in fact it was way simpler than I thought !
            And this just proves how wonderfull ModX is.

            So, for anyone looking to do the same, here is how I do it :
            First, I use Mootools 1.2 with Request lib. You have to include it in your page.
            You also have to include the following javascript code :
            window.addEvent('domready', function() {
            var reqlogin = new Request.JSON({url:'connectors/security/login.php', method:'post',
                    onSuccess: function(resultatjson) {
                        if(resultatjson.success) {
                            window.location.reload(true);
                        } else {
                            $('mtamessage').set('text', resultatjson.message);
                        }
                    },
                    //Our request will most likely succeed, but just in case, we'll add an
                    //onFailure method which will let the user know what happened.
                    onFailure: function(resultatjson) {
                            $('mtamessage').set('text', 'The request failed.');
                    }
                });
            
            var reqlogout = new Request.JSON({url:'connectors/security/logout.php',
                    onSuccess: function(resultatjson) {
                        window.location.reload(true);
                    },
                    //Our request will most likely succeed, but just in case, we'll add an
                    //onFailure method which will let the user know what happened.
                    onFailure: function() {
                            $('mtamessage').set('text', 'The request failed.');
                    }
                });
               
                if($defined($('LoginRequest')))
                $('LoginRequest').addEvent('click', function(evt) {
                    new Event(evt).stop();
                    reqlogin.send('username='+($('mtausername').value)+'&password='+($('mtaPassword').value)+'&login_context=web&rememberme='+($('mtaremember').checked));
                });
            
                if($defined($('LogoutRequest')))
                $('LogoutRequest').addEvent('click', function(evt) {
                    new Event(evt).stop();
                    reqlogout.send();
                });
            });


            Then, using the Personalize snippet, I have the following chunks :
            yesChunk :
            Logged : [[+username]]
            <div id="mtamessage" style="color:red"></div>
            <form method="POST">
            <button type="submit" id="LogoutRequest" value="logout">Logout</button>
            </form>


            noChunk :
            <strong>Log In : </strong>
            <form method="post">
            <div id="mtamessage" style="color:red"></div>
            <label id="mtausernameLabel" for="mtausername">Username
            <input id="mtausername" type="text" name="username" />
            </label>
            <label id="mtaPasswordLabel" for="mtaPassword">Password
            <input id="mtaPassword" type="password" name="password" />
            </label>
            <input id="mtacontext" type="hidden" name="login_context" value="web"/>
            <label id="mtarememberLabel" for="mtaremember">Remember me
            <input id="mtaremember" type="checkbox" name="rememberme"/>
            </label>
            <button type="submit" id="LoginRequest" value="login">Login</button>
            </form>


            Now, it seems you can’t use the same session on the Web context and in admin mode, so you have to logout, then login in the manager to be able to use it. I think it would be best if the session was unique, but that’s not a great deal anyway.
            • Quote from: Thalion at Nov 02, 2008, 02:26 PM

              Now, it seems you can’t use the same session on the Web context and in admin mode, so you have to logout, then login in the manager to be able to use it. I think it would be best if the session was unique, but that’s not a great deal anyway.
              MODx sessions can include logging-in/logging-out of any number of contexts individually, without affecting any of the others. You could even write a script to login to or logout of multiple contexts at once (the login processor does this when logging into the ’mgr’ context by automatically adding a ’connector’ context authentication token to the session).