Question: 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](https://www.drupal.org/node/1431696), I need to tweak the services modules to achieve this requirement.

Is there any way to achieve this?

Answer: 
You can do this in a *custom module*:

```
/**
 * Implements hook_services_resources_alter().
 */
function my_module_services_resources_alter(&$resources, &$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' => $username)
  )->fetch();
  if (!empty($result->name)) { $username = $result->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>
For time-being I've added my own patch in my services module.

In the user\_resource.inc file, added the following line of code in the `_user_resource_login` function, just before the call to `user_authenticate`

```
if (valid_email_address($username)) { 
$username = db_query("SELECT name FROM {users} WHERE mail = :mail",array(':mail' => $username))->fetchField();
}

```

I've tested both following formats to login the user, working great.

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

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

```

I'll try to post @Clives suggestion (from comments) as my answer once I get some time :)

I hope this helps for someone who looks to add their own patch to their services module