MyBB Community Forums

Full Version: Logging in with email address
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Please make sure you read the entire post before asking for help. I've posted the original function so that you can see the differences in the event these functions change you can manually apply the differences.

If you wish to allow logging in with email address but not username
In functions_user.php:
/**
 * Checks a password with a supplied username.
 *
 * @param string The username of the user.
 * @param string The plain-text password.
 * @return boolean|array False when no match, array with user info when match.
 */
function validate_password_from_username($username, $password)
{
	global $db;

	$query = $db->simple_select("users", "uid,username,password,salt,loginkey,coppauser,usergroup", "LOWER(username)='".$db->escape_string(my_strtolower($username))."'", array('limit' => 1));

	$user = $db->fetch_array($query);
	if(!$user['uid'])
	{
		return false;
	}
	else
	{
		return validate_password_from_uid($user['uid'], $password, $user);
	}
}

Becomes:
/**
 * Checks a password with a supplied username.
 *
 * @param string The username of the user.
 * @param string The plain-text password.
 * @return boolean|array False when no match, array with user info when match.
 */
function validate_password_from_username($username, $password)
{
	global $db;

	$query = $db->simple_select("users", "uid,username,email,password,salt,loginkey,coppauser,usergroup", "LOWER(email)='".$db->escape_string(my_strtolower($username))."'", array('limit' => 1));

	$user = $db->fetch_array($query);
	if(!$user['uid'])
	{
		return false;
	}
	else
	{
		return validate_password_from_uid($user['uid'], $password, $user);
	}
}


If you wish to allow logging in with email address or username
In functions_user.php:
/**
 * Checks a password with a supplied username.
 *
 * @param string The username of the user.
 * @param string The plain-text password.
 * @return boolean|array False when no match, array with user info when match.
 */
function validate_password_from_username($username, $password)
{
	global $db;

	$query = $db->simple_select("users", "uid,username,password,salt,loginkey,coppauser,usergroup", "LOWER(username)='".$db->escape_string(my_strtolower($username))."'", array('limit' => 1));

	$user = $db->fetch_array($query);
	if(!$user['uid'])
	{
		return false;
	}
	else
	{
		return validate_password_from_uid($user['uid'], $password, $user);
	}
}

Becomes:
/**
 * Checks a password with a supplied username.
 *
 * @param string The username of the user.
 * @param string The plain-text password.
 * @return boolean|array False when no match, array with user info when match.
 */
function validate_password_from_username($username, $password)
{
	global $db;

	$query = $db->simple_select("users", "uid,username,email,password,salt,loginkey,coppauser,usergroup", "LOWER(username)='".$db->escape_string(my_strtolower($username))."' OR LOWER(email)='".$db->escape_string(my_strtolower($username))."'", array('limit' => 1));

	$user = $db->fetch_array($query);
	if(!$user['uid'])
	{
		return false;
	}
	else
	{
		return validate_password_from_uid($user['uid'], $password, $user);
	}
}


For both
In functions_user.php
/**
 * Checks if $username already exists in the database.
 *
 * @param string The username for check for.
 * @return boolean True when exists, false when not.
 */
function username_exists($username)
{
        global $db;

        $query = $db->simple_select("users", "COUNT(*) as user", "LOWER(username)='".$db->escape_string(my_strtolower($username))."'", array('limit' => 1));

        if($db->fetch_field($query, 'user') == 1)
        {
                return true;
        }
        else
        {   
                return false;
        }
}

Becomes:
/**
 * Checks if $username already exists in the database.
 *
 * @param string The username for check for.
 * @return boolean True when exists, false when not.
 */
function username_exists($username)
{
	global $db;

	$query = $db->simple_select("users", "COUNT(*) as user,email,username", "LOWER(username)='".$db->escape_string(my_strtolower($username))."' OR LOWER(email)='".$db->escape_string(my_strtolower($username))."'", array('limit' => 1));

	if($db->fetch_field($query, 'user') == 1)
	{
		return true;
	}
	else
	{
		return false;
	}
}
Stickied per multiple requests.

Also, I've brought up to the team to possibly include this in the core in a future revision.
Haven't tested but its looking perfect Dylan.
(2011-10-14, 04:27 PM)Yaldaram Wrote: [ -> ]Haven't tested but its looking perfect Dylan.

Thanks Wink
demo? Big Grin
(2011-10-14, 05:17 PM)Solstice Wrote: [ -> ]demo? Big Grin

Sorry, no demo Toungue
It does work though, the original thread this was requested in had 2 different people report it working.
Perfect, all websites, even social network allows it, MyBB for the win.
Wow, i not have tested seen in MyBB like e-mail login. Good work Dylan M. - Keep up Wink
Now implemented into core for 1.6.6 - assuming it passes SQA since it is a very late night for me Wink
[Issue #1853]
That'd be very cool Dylan. Would there be an option to set logging with email as yes or no?
Pages: 1 2 3