Refactor the login attempts + add tests (fixes #14874).