User Login with Email Address - Services Drupal login form allows users to enter their username or email to login into the site.

I need to achieve the same thing with services. But services allows users to login with username (not working with email).

JSON format for User Login (works).

{"username":'myusername', "password":"mypassword"}


But how can we do login using "email" as username? I've tried the following JSON formats

Input:

{"username":'myusername@gmail.com', "password":"mypassword"}


Response:

["Wrong username or password."]

Input:

{"mail":'myusername@gmail.com', "password":"mypassword"}


Response:

["Missing required argument username"]


According to this link, I need to tweak the services modules to achieve this requirement.

Is there any way to achieve this?
You can do this in a custom module:

/**
 * Implements hook_services_resources_alter().
 */
function my_module_services_resources_alter(&amp;$resources, &amp;$endpoint) {
  // Override the normal user login callback.
  if (isset($resources['user']['actions']['login']['callback'])) {
    $resources['user']['actions']['login']['callback'] = 'my_module_services_login';
  }
}

/**
 * Replacement callback for the Services user login resource. Optionally use the email address to login.
 *
 * @see _user_resource_login().        
 * @see https://drupal.org/node/1431724
 */
function my_module_services_login($username, $password) {
  $result = db_query(
    "SELECT name FROM {users} WHERE LOWER(mail) = LOWER(:username)",
    array(':username' =&gt; $username)
  )-&gt;fetch();
  if (!empty($result-&gt;name)) { $username = $result-&gt;name; }
  return _user_resource_login($username, $password);
}


Using this technique, you don't need to patch the Services module.

This solution is borrowed from DrupalGap:

http://cgit.drupalcode.org/drupalgap/tree/modules/drupalgap_logintoboggan/drupalgap_logintoboggan.module