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


{"username":'[email protected]', "password":"mypassword"}


["Wrong username or password."]


{"mail":'[email protected]', "password":"mypassword"}


["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?

  • 1
    Short answer: yes, just write a custom service resource which uses logintoboggan's authentication method rather than core's, and use that to login/get session ids/etc from your client app.
    – Clive
    Commented Dec 2, 2015 at 13:51

2 Answers 2


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)
  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:



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":"[email protected]","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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.