[PR #2658] [CLOSED] fix(2fa): Return correct error for bad TOTP code in 2FA setup #30029

Closed
opened 2026-04-17 21:13:58 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/2658
Author: @obendev
Created: 5/14/2025
Status: Closed

Base: mainHead: fix/2631-2fa-setup-error


📝 Commits (1)

  • c7acfee fix(2fa): Return correct error for bad TOTP code in 2FA setup

📊 Changes

1 file changed (+11 additions, -10 deletions)

View changed files

📝 packages/better-auth/src/plugins/two-factor/verify-two-factor.ts (+11 -10)

📄 Description

When a logged-in user was setting up 2FA (TOTP) and submitted an incorrect code, they would receive a misleading INVALID_TWO_FACTOR_COOKIE error. This happened because the verifyTwoFactor helper, in cases where a session already existed, was hardcoding this error in its invalid handler.

This change modifies the invalid handler in verifyTwoFactor (for the "session exists" path) to accept and use the errorKey provided by the calling function. This allows the verifyTOTP endpoint handler to correctly signal an INVALID_CODE (or similar specific error) when a user enters the wrong TOTP.

As a small improvement, the invalid handler logic within verifyTwoFactor has also been refactored into a shared function to reduce duplication.

Closes #2631.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/2658 **Author:** [@obendev](https://github.com/obendev) **Created:** 5/14/2025 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/2631-2fa-setup-error` --- ### 📝 Commits (1) - [`c7acfee`](https://github.com/better-auth/better-auth/commit/c7acfee3c948c339a192416e168152a7ee96ebd5) fix(2fa): Return correct error for bad TOTP code in 2FA setup ### 📊 Changes **1 file changed** (+11 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/two-factor/verify-two-factor.ts` (+11 -10) </details> ### 📄 Description When a logged-in user was setting up 2FA (TOTP) and submitted an incorrect code, they would receive a misleading `INVALID_TWO_FACTOR_COOKIE` error. This happened because the `verifyTwoFactor` helper, in cases where a session already existed, was hardcoding this error in its `invalid` handler. This change modifies the `invalid` handler in `verifyTwoFactor` (for the "session exists" path) to accept and use the `errorKey` provided by the calling function. This allows the `verifyTOTP` endpoint handler to correctly signal an `INVALID_CODE` (or similar specific error) when a user enters the wrong TOTP. As a small improvement, the `invalid` handler logic within `verifyTwoFactor` has also been refactored into a shared function to reduce duplication. Closes #2631. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-17 21:13:58 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#30029