Password login fails with "The selected two-factor method is not enabled" for users who have a passkey but no 2FA

AI-generated summary

Overgrow reports a bug where users who have set up a passkey but no other two-factor authentication (2FA) method (such as an authenticator app or security key) are unable to log in using their username and password. Instead, the login fails with the error message The selected two-factor method is not enabled for your account (not_enabled_second_factor_method). Passkey and email code logins remain functional.

The root cause is identified in SecondFactorManager#authenticate_second_factor. The login form always sends a default second factor method (TOTP). When allow_passkeys_for_2fa is enabled, the presence of a passkey makes passkeys_available_as_second_factor? true, causing the early return check to fail. Consequently, the system attempts to validate the TOTP method for a user who does not have it configured, resulting in the error. This regression was introduced in PR #39674.

To fix the issue, the logic could be updated to only fall through to second factor validation if the request explicitly asks for a passkey (params[:second_factor_method].to_i != UserSecondFactor.methods[:passkey]), or the login form could be modified to leave second_factor_method empty until the server explicitly requires a second factor.

As a temporary workaround, site administrators can disable the allow_passkeys_for_2fa setting. This restores standard password login functionality, though it disables the ability to use passkeys as a second factor on the /session/2fa endpoint.

Members who have added a passkey but have no authenticator app or security key can’t log in with username and password any more. Every attempt fails with The selected two-factor method is not enabled for your account. Logging in with the passkey, or with an email code, still works.

Steps to reproduce

  1. Leave enable_passkeys and allow_passkeys_for_2fa at their defaults (both on).
  2. As a user with no 2FA, add a passkey in Preferences > Security.
  3. Log out, then log in with username and password.

Expected: the user is logged in. They have no second factor, so none should be asked for.

Actual: the login is refused with not_enabled_second_factor_method, every time.

Cause

The login form always posts a second factor method, TOTP by default (frontend/discourse/app/controllers/login.js, secondFactorMethod = SECOND_FACTOR_METHODS.TOTP, sent as second_factor_method).

SecondFactorManager#authenticate_second_factor only returns early when

!requires_second_factor_for_login? &&
  (!passkeys_available_as_second_factor? || params[:second_factor_method].blank?)

A passkey makes passkeys_available_as_second_factor? true, and the form makes the method non-blank. So the check falls through to valid_second_factor_method_for_user?(TOTP), which fails because the user has no TOTP.

This came in with #39674 (passkeys as 2FA on /session/2fa) and is still the same on main today.

Possible fix

Only fall through when the request really asks for a passkey:

if !requires_second_factor_for_login? &&
     (
       !passkeys_available_as_second_factor? ||
         params[:second_factor_method].to_i != UserSecondFactor.methods[:passkey]
     )
  return ok_result
end

Or have the login form leave second_factor_method empty until the server asks for a second factor.

Workaround for anyone hitting this: turn off allow_passkeys_for_2fa. Password login works again straight away, and “Log in with a passkey” keeps working. You lose passkeys as a 2FA step on /session/2fa.