<![CDATA[ Any way to login using Email address with Login plugin? - My Forums]]> https://forums.modx.com/thread/?thread=96586 <![CDATA[Any way to login using Email address with Login plugin?]]> https://forums.modx.com/thread/96586/any-way-to-login-using-email-address-with-login-plugin#dis-post-522748
Would anyone know if this is possible and, if so, how?]]>
meltingdog Mar 17, 2015, 01:42 AM https://forums.modx.com/thread/96586/any-way-to-login-using-email-address-with-login-plugin#dis-post-522748
<![CDATA[Re: Any way to login using Email address with Login plugin?]]> https://forums.modx.com/thread/96586/any-way-to-login-using-email-address-with-login-plugin#dis-post-522851
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)]]>
chris.dempsey78 Mar 17, 2015, 01:15 PM https://forums.modx.com/thread/96586/any-way-to-login-using-email-address-with-login-plugin#dis-post-522851
<![CDATA[Re: Any way to login using Email address with Login plugin? (Best Answer)]]> https://forums.modx.com/thread/96586/any-way-to-login-using-email-address-with-login-plugin#dis-post-522774
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);
}
]]>
meltingdog Mar 17, 2015, 06:27 AM https://forums.modx.com/thread/96586/any-way-to-login-using-email-address-with-login-plugin#dis-post-522774
<![CDATA[Re: Any way to login using Email address with Login plugin?]]> https://forums.modx.com/thread/96586/any-way-to-login-using-email-address-with-login-plugin#dis-post-522772 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).]]>
meltingdog Mar 17, 2015, 06:17 AM https://forums.modx.com/thread/96586/any-way-to-login-using-email-address-with-login-plugin#dis-post-522772
<![CDATA[Re: Any way to login using Email address with Login plugin?]]> https://forums.modx.com/thread/96586/any-way-to-login-using-email-address-with-login-plugin#dis-post-522762
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;
]]>
BobRay Mar 17, 2015, 03:21 AM https://forums.modx.com/thread/96586/any-way-to-login-using-email-address-with-login-plugin#dis-post-522762
<![CDATA[Re: Any way to login using Email address with Login plugin?]]> https://forums.modx.com/thread/96586/any-way-to-login-using-email-address-with-login-plugin#dis-post-522752 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]]>
meltingdog Mar 17, 2015, 02:18 AM https://forums.modx.com/thread/96586/any-way-to-login-using-email-address-with-login-plugin#dis-post-522752
<![CDATA[Re: Any way to login using Email address with Login plugin?]]> https://forums.modx.com/thread/96586/any-way-to-login-using-email-address-with-login-plugin#dis-post-522750
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.


]]>
sottwell Mar 17, 2015, 02:11 AM https://forums.modx.com/thread/96586/any-way-to-login-using-email-address-with-login-plugin#dis-post-522750