[GH-ISSUE #2631] Bug: INVALID_TWO_FACTOR_COOKIE error during 2FA setup when skipVerificationOnEnable is false #17911

Closed
opened 2026-04-15 16:16:07 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @obendev on GitHub (May 12, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2631

Current Behavior:

When a logged-in user is setting up Two-Factor Authentication (TOTP) and submits an incorrect TOTP code for verification, the server unexpectedly responds with an INVALID_TWO_FACTOR_COOKIE error. This occurs even though the user has an active session, and the actual issue is the incorrect TOTP code.

Steps to Reproduce:

  1. A user is logged in (active session).
  2. User initiates 2FA (TOTP) setup, receives a QR code.
  3. User scans the QR code and then intentionally enters an incorrect TOTP code to verify the setup.
  4. Client submits this incorrect code to the endpoint.
  5. Server responds with an INVALID_TWO_FACTOR_COOKIE error.

Expected Behavior:

When a logged-in user submits an incorrect TOTP code during the 2FA setup verification step, the server should respond with an error that accurately reflects the incorrect code, such as INVALID_CODE or INVALID_TOTP.

Actual Behavior:

The server returns INVALID_TWO_FACTOR_COOKIE. This is misleading because:

  • The user's session is active.
  • The error suggests an issue with a linking cookie (typically used in no-session 2FA challenges), not with the entered TOTP code itself.
  • This prevents the client from providing accurate feedback (e.g., "Incorrect code" vs. "Session issue").

Root Cause Analysis:

The endpoint handler (in packages/better-auth/src/plugins/two-factor/totp/index.ts) correctly identifies when a TOTP code is incorrect and attempts to signal this by calling an invalid("INVALID_CODE") handler.

However, the verifyTwoFactor helper function (in packages/better-auth/src/plugins/two-factor/verify-two-factor.ts), when an active session already exists, provides an invalid handler that is currently hardcoded to throw INVALID_TWO_FACTOR_COOKIE, regardless of the specific reason for invalidity passed by the calling function. This overrides the more specific error intended by the verifyTOTP logic.

Impact:

This issue leads to a confusing user experience and makes it harder for client applications to guide users appropriately when they make a mistake entering their TOTP code during 2FA setup.

Environment:

  • Better Auth Version: 1.2.7
  • Framework: Next.js 15.3 (Adjust as needed)
  • Node.js Version: 22
  • Browser: Chrome 136
Originally created by @obendev on GitHub (May 12, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2631 **Current Behavior:** When a logged-in user is setting up Two-Factor Authentication (TOTP) and submits an incorrect TOTP code for verification, the server unexpectedly responds with an `INVALID_TWO_FACTOR_COOKIE` error. This occurs even though the user has an active session, and the actual issue is the incorrect TOTP code. **Steps to Reproduce:** 1. A user is logged in (active session). 2. User initiates 2FA (TOTP) setup, receives a QR code. 3. User scans the QR code and then intentionally enters an **incorrect TOTP code** to verify the setup. 4. Client submits this incorrect code to the endpoint. 5. Server responds with an `INVALID_TWO_FACTOR_COOKIE` error. **Expected Behavior:** When a logged-in user submits an incorrect TOTP code during the 2FA setup verification step, the server should respond with an error that accurately reflects the incorrect code, such as `INVALID_CODE` or `INVALID_TOTP`. **Actual Behavior:** The server returns `INVALID_TWO_FACTOR_COOKIE`. This is misleading because: * The user's session is active. * The error suggests an issue with a linking cookie (typically used in no-session 2FA challenges), not with the entered TOTP code itself. * This prevents the client from providing accurate feedback (e.g., "Incorrect code" vs. "Session issue"). **Root Cause Analysis:** The endpoint handler (in `packages/better-auth/src/plugins/two-factor/totp/index.ts`) correctly identifies when a TOTP code is incorrect and attempts to signal this by calling an `invalid("INVALID_CODE")` handler. However, the `verifyTwoFactor` helper function (in `packages/better-auth/src/plugins/two-factor/verify-two-factor.ts`), when an active session already exists, provides an `invalid` handler that is currently hardcoded to throw `INVALID_TWO_FACTOR_COOKIE`, regardless of the specific reason for invalidity passed by the calling function. This overrides the more specific error intended by the `verifyTOTP` logic. **Impact:** This issue leads to a confusing user experience and makes it harder for client applications to guide users appropriately when they make a mistake entering their TOTP code during 2FA setup. **Environment:** * Better Auth Version: 1.2.7 * Framework: Next.js 15.3 (Adjust as needed) * Node.js Version: 22 * Browser: Chrome 136
GiteaMirror added the locked label 2026-04-15 16:16:07 -05:00
Author
Owner

@obendev commented on GitHub (Jun 21, 2025):

Code for the fix can be found in https://github.com/better-auth/better-auth/pull/2658

<!-- gh-comment-id:2993723135 --> @obendev commented on GitHub (Jun 21, 2025): Code for the fix can be found in https://github.com/better-auth/better-auth/pull/2658
Author
Owner

@budivoogt commented on GitHub (Jun 25, 2025):

Thanks for creating this issue @obendev. I'm also using the default for skipVerificationOnEnabled and am actually running into this issue when the TOTP is verified successfully on the first try. While twoFactorEnabled is set to TRUE for that user, I can not successfully verify any of that user's TOTP entries server-side after they have setup and verified 2FA for the first time. Do you happen to know how I can resolve that issue? It sounds like you have gotten 2FA to work in instances where the user verifies successfully on the first try during setup.

<!-- gh-comment-id:3004992660 --> @budivoogt commented on GitHub (Jun 25, 2025): Thanks for creating this issue @obendev. I'm also using the default for `skipVerificationOnEnabled` and am actually running into this issue when the TOTP is verified successfully on the first try. While `twoFactorEnabled` is set to `TRUE` for that user, I can not successfully verify any of that user's TOTP entries server-side after they have setup and verified 2FA for the first time. Do you happen to know how I can resolve that issue? It sounds like you have gotten 2FA to work in instances where the user verifies successfully on the first try during setup.
Author
Owner

@dosubot[bot] commented on GitHub (Sep 24, 2025):

Hi, @obendev. I'm Dosu, and I'm helping the better-auth team manage their backlog and am marking this issue as stale.

Issue Summary

  • You reported that with 2FA (TOTP) setup and skipVerificationOnEnable set to false, submitting an incorrect code returned an INVALID_TWO_FACTOR_COOKIE error instead of INVALID_CODE.
  • This caused complications for client-side error handling in Better Auth v1.2.7.
  • You provided a fix in PR #2658 that corrected the error handling in the verifyTwoFactor helper.
  • Another user, budivoogt, raised a related question about verifying TOTP entries after the initial setup, which remains open.

Next Steps

  • Please confirm if this issue and the related TOTP verification question are still relevant with the latest version of better-auth by commenting here.
  • If no further updates are provided, I will automatically close this issue in 7 days.

Thanks for your understanding and your valuable contribution!

<!-- gh-comment-id:3329676433 --> @dosubot[bot] commented on GitHub (Sep 24, 2025): Hi, @obendev. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog and am marking this issue as stale. **Issue Summary** - You reported that with 2FA (TOTP) setup and `skipVerificationOnEnable` set to false, submitting an incorrect code returned an `INVALID_TWO_FACTOR_COOKIE` error instead of `INVALID_CODE`. - This caused complications for client-side error handling in Better Auth v1.2.7. - You provided a fix in PR #2658 that corrected the error handling in the `verifyTwoFactor` helper. - Another user, budivoogt, raised a related question about verifying TOTP entries after the initial setup, which remains open. **Next Steps** - Please confirm if this issue and the related TOTP verification question are still relevant with the latest version of better-auth by commenting here. - If no further updates are provided, I will automatically close this issue in 7 days. Thanks for your understanding and your valuable contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#17911