13

I can't find if it is possible or not to change the log in system by using the couple (email, password) instead of (username, password).

I think people never remember their usernames when not available as they wanted and so had to add a number at the end of it ...

20

Both the Login Toboggan and Email Registration modules allow your users to use their email when logging in.

1
0

The developer has moved a module that meets your need from the sandbox to the project page: https://www.drupal.org/project/login_email

The module exists as 7.x-1.x-dev It works as is...looks like a bit of assistance can turn this into an official release.

0

In order to allow both username and email: first add validator

function YOURMODULE_form_user_login_alter(&$form, &$form_state, $form_id) {
    $form['#validate'][1] = 'YOURMODULE_form_user_login_replace_email_validate';
    $form['#validate'][2] = 'user_login_authenticate_validate';
    $form['#validate'][3] = 'user_login_final_validate';
}

then check if looks like email address (contains @, letters before and after @ and at least 1 '.' after @). If it is, than replace email by username:

function YOURMODULE_form_user_login_replace_email_validate(&$form, &$form_state) {

    $name = $form_state['values']['name'];
    if (strpos($name, '@')) {
        $mailParts = explode('@', $name);
        if ((int)count($mailParts) === 2) {
            $extParts = explode('.', $mailParts[1]);
            if (count($extParts) > 1) {
                $query = db_select('users', 'u');
                $query->distinct();
                $query->fields('u', ['uid']);
                $query->fields('u', ['name']);
                $query->fields('u', ['mail']);
                $query->condition('u.mail', $name, '=');
                $result = $query->execute()->fetchAllAssoc('name');

                if (!empty($result)) {
                    $name = array_keys($result)[0];
                    $form_state['values']['name'] = $name;
                }
            }
        }
    }
}

I had a specific situation when I had to add email-based authentification to working site with a lot of users leaving the "old" authentification working, and I did it in such a way.

0

Here's how to let a user login with their email address in Drupal 7:

/**
 * Implements hook_form_alter().
 */
function MY_MODULE_form_alter(&$form, $form_state, $form_id) {

  // On the user login form...
  if ($form_id == 'user_login') {
  
    // OPTIONAL: Change the title on the user name input.
    $form['name']['#title'] = t('E-mail address');

    // OPTIONAL: Clear the descriptions on the user name and pass inputs.
    $form['name']['#description'] = '';
    $form['pass']['#description'] = '';

    // Prepend a custom validation handler.
    array_unshift($form['#validate'], 'MY_MODULE_user_login_form_validate');

  }

}

/**
 * A custom validation handler for the user login form.
 */
function MY_MODULE_user_login_form_validate(&$form, &$form_state) {

  // If they provided a valid email address for the user name...
  $name = $form_state['values']['name'];
  if (valid_email_address($name)) {
  
    // If an account exists for the email address...
    $account = user_load_by_mail($name);
    if ($account) {
    
      // Set the account's user name onto the form state.
      form_set_value($form['name'], $account->name, $form_state);

    }

  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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