Skip to content

[5.x]: E-Mail can take precedence over username in login form #18148

@MoritzLost

Description

@MoritzLost

What happened?

Description

I'm seeing an issue where a user can't log in with their username, if there's another user that has a different username, but the same e-mail. Even if that other user is disabled.

Steps to reproduce

  1. Set useEmailAsUsername to false in config/general.php.
  2. Create a user with username info@example.com and E-Mail info@example.com. Activate the user.
  3. Create another user with username bob and username info@example.com. Leave it deactivated.
  4. Try to log in with info@example.com. It may work or it may not, because the database may return either user (there's nothing in the query to order the users, so it's up to the database).

This may seem like a contrived example, but we're seeing this in the real world. In this case, the e-mail is a shared mailbox and the users are synced from an external system.

Expected behavior

I suggest two changes (ideally, I would like to see both implemented):

  1. If useEmailAsUsername is set to false, the username should always take precedence over the email field. That is, if a user tries to log in, the system should check if there's a match in the username field before checking matches in the email field.
  2. If two or more accounts have the same email, but one is disabled, the login should always favor the account that is enabled.

Actual behavior

The query in User::getUserByUsernameOrEmail() just tries to find any user where the username or email match the input, so the login may or may not work.

Craft CMS version

5.8.18

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions