3

I'm using realname with this small moduel to generate username from some profile field.

Now users can login with both username and email by using LoginToboggan.

Only the problem is that I don't want to let users login with their username. It looks like LoingToboggan has the function to let users login with email but not disable username login.

Is their anyway I can just disable username login?

3

I don't know of an existing module that does this, but it would only take a few lines of code in a custom module to get the effect you're looking for:

function MYMODULE_form_user_login_alter(&$form, &$form_state) {
  // Add an extra validation step to the form.
  // The user is actually authenticated during the validation stage
  // so we have to make sure this validate function runs before any others
  array_unshift($form['#validate'], 'MYMODULE_user_login_validate');
}

function MYMODULE_user_login_validate($form, &$form_state) {
  // Make sure the provided username field is in the form of an email address.
  // If not we can safely say the user shouldn't be allowed to log in,
  // regardless of whether or not a user exists with that username.
  if (!valid_email_address($form_state['values']['name'])) {
    form_set_error('name', 'The email address you have entered was not found in the system.');

    // Empty the name variable so further validation definitely fails.
    // There are probably better ways to do this so be creative :)
    $form_state['values']['name'] = '';
  }
}
  • Thanks,I tried your code but it only display the error message when a users put their username but it actually lets them login. – chinita7 Mar 18 '12 at 12:39
  • @chinita7 Sorry about that, turns out the user is authenticated during the validation stage so you need to make sure the new validate function is run first. The updated code should sort it out :) – Clive Mar 18 '12 at 13:13
  • I tried it again your code just changing the module name but now the site doesn't allow users to login at all even with registered emails.. Is there any reason you can think of? Thanks. – chinita7 Mar 18 '12 at 16:24

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.