We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40122
    • 330 Posts
    I've built a site with the Login plugin and it works great. However, the client has requested that users be able to login user their email address instead of username. I checked out the Login RTFM but couldnt see anyway of achieving this.

    Would anyone know if this is possible and, if so, how?

    This question has been answered by meltingdog. See the first response.

    • You can use the email address as the username to begin with, but I don't know of any way to use the email address if the user is already set up to have a separate username and email address.

      I suppose you could create a plugin using OnBeforeWebLogin to take the email from the login form, get that user's username and replace the form value with the username and complete the login. But unless you need a separate username for some reason, it would be a lot easier to just use the email address for the username in the first place.


        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
        • 40122
        • 330 Posts
        Quote from: sottwell at Mar 17, 2015, 07:11 AM
        You can use the email address as the username to begin with, but I don't know of any way to use the email address if the user is already set up to have a separate username and email address.

        I suppose you could create a plugin using OnBeforeWebLogin to take the email from the login form, get that user's username and replace the form value with the username and complete the login. But unless you need a separate username for some reason, it would be a lot easier to just use the email address for the username in the first place.



        Thanks Sottwell! Yeah I think I'll have to make something with AJAX where it queries the database and returns their username when they try to login. I will post my code here once done
          • 3749
          • 24,544 Posts
          You shouldn't need Ajax, just a plugin attached to OnWebAuthentication (and/or OnManagerAuthentication).

          Something like this (untested):

          if (strpos($username, '@') !== false) {
              $profile = $modx->getObject('modUserProfile', array('email' => $username));
              if ($profile) {
                  $usr = $profile->getOne('User');
                  if ($usr) {
                     if ($usr->passwordMatches($password)) {
                        return true;
                     }
                  }
              }
          }
          
          return false;
            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
            • 40122
            • 330 Posts
            Quote from: BobRay at Mar 17, 2015, 08:21 AM
            You shouldn't need Ajax, just a plugin attached to OnWebAuthentication (and/or OnManagerAuthentication).

            Thanks BobRay, but this is probably a bit too advanced for me (I'm not very familiar with Modx's inner workings).
            • discuss.answer
              • 40122
              • 330 Posts
              If anyone is interested I was able to do this by using AJAX. There is probably a much better way of doing this but I am not sure how.

              Modified lgnLoginTpl:

              <div class="loginForm">
                  <div class="loginMessage">[[+errors]]</div>
                  <div class="loginLogin">
                      <form class="loginLoginForm" action="[[~[[*id]]]]" method="post">
                          <fieldset class="loginLoginFieldset">
                              <legend class="loginLegend">[[+actionMsg]]</legend>
                              
                              <label class="loginEmailLabel">Email Address
                                  <input class="loginEmail" type="text" name="useremail" />
                              </label>
                              
                                  <input class="loginUsername" type="hidden" name="username" />
                              
                              <label class="loginPasswordLabel">[[%login.password]]
                                  <input class="loginPassword" type="password" name="password" />
                              </label>
                              <input class="returnUrl" type="hidden" name="returnUrl" value="[[+request_uri]]" />
              
                              [[+login.recaptcha_html]]
                              
                              <input class="loginLoginValue" type="hidden" name="service" value="login" />
                              <span class="loginLoginButton"><input type="submit" name="Login" value="[[+actionMsg]]" /></span>
                          </fieldset>
                      </form>
                  </div>
              </div>




              AJAX Chunk:

              <script type="text/javascript">
              $(document).ready(function() {
              $('.loginEmail').focusout(function(e) {
                 var url = 'URL OF PAGE WITH AJAX PHP';
                 var data = {
                    userEmail: $(".loginEmail").val(),
                   };//DATA
                   
                  var success = function(data) {
              		var username = data.msg;
              		$('.loginUsername').val(username);
                  }//SUCCESS
              
              	$.ajax(url, {
              		type: 'get',
              		dataType: 'json',
              		data: data,
              		success: success
              	});
               });//FOCUS OUT
              });//READY
                  
                  
              </script>



              AJAX PHP

              <?php
              $useremail = trim($_GET['userEmail']);
              
              //FIND USER NAME
              $query = "SELECT * FROM `modx_user_attributes` WHERE `email` = '$useremail'";
              
              $result = $modx->query($query);
              
              if ($result) {
                   
                  while ($resultrow = $result->fetch(PDO::FETCH_ASSOC)) {
                      $userid = $resultrow['id'];
                      
                      $query2 = "SELECT * FROM `modx_users` WHERE `id` = '$userid'";
                      $result2 = $modx->query($query2);
                      
                      if ($result2) {
                          while ($result2row = $result2->fetch(PDO::FETCH_ASSOC)) {
                              $username = $result2row['username'];
                              sendSuccess($username);
                          }
                      }
                        
                  }
              }
              
              function sendSuccess($msg) {
              	$result = array('res' => 1, 'msg' => $msg);
              	sendResponse($result);
              }
                • 5160
                • 118 Posts
                chris.dempsey78 Reply #7, 9 years ago
                You probably need to sanitise $useremail more thoroughly than that.

                In my tired state, and viewing on a phone it looks like you're putting user input directly into a MySQL query.

                Maybe with

                if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false)