[PR #5174] [MERGED] fix(two-factor): return parsed array in viewBackupCodes #22726

Closed
opened 2026-04-15 21:14:19 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5174
Author: @ahmed-abdat
Created: 10/8/2025
Status: Merged
Merged: 10/9/2025
Merged by: @himself65

Base: canaryHead: fix/backup-codes-json-parse-error


📝 Commits (5)

  • 3066943 fix(two-factor): return parsed backup codes array in viewBackupCodes endpoint
  • f622644 refactor: use ES6 shorthand property syntax for backupCodes
  • 56cd0c6 fix(two-factor): handle all storage modes in viewBackupCodes
  • 15e01da test(two-factor): add viewBackupCodes regression tests
  • 0a00273 Merge remote-tracking branch 'upstream/canary' into fix/backup-codes-json-parse-error

📊 Changes

2 files changed (+84 additions, -6 deletions)

View changed files

📝 packages/better-auth/src/plugins/two-factor/backup-codes/index.ts (+6 -6)
📝 packages/better-auth/src/plugins/two-factor/two-factor.test.ts (+78 -0)

📄 Description

Fixes #5168

Problem

When calling auth.api.viewBackupCodes() after generating new backup codes, users get a JSON parse error:

SyntaxError: Unexpected number in JSON at position 1

The viewBackupCodes endpoint was missing the decryptBackupCodes() call, causing it to fail when storage encryption is enabled.

Solution

Added proper dual-layer decryption to handle all storage configurations:

  • decryptBackupCodes() removes optional storage encryption layer
  • getBackupCodes() removes internal encryption and parses JSON

This matches the pattern used in the verifyBackupCode endpoint.

Tests

  • All 17 two-factor tests pass
  • Handles plain/undefined storage mode
  • Handles encrypted storage mode
  • Handles custom encryption functions

🔄 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/5174 **Author:** [@ahmed-abdat](https://github.com/ahmed-abdat) **Created:** 10/8/2025 **Status:** ✅ Merged **Merged:** 10/9/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/backup-codes-json-parse-error` --- ### 📝 Commits (5) - [`3066943`](https://github.com/better-auth/better-auth/commit/3066943e92b925b65c79675569937880b4924eb8) fix(two-factor): return parsed backup codes array in viewBackupCodes endpoint - [`f622644`](https://github.com/better-auth/better-auth/commit/f62264402f2a9913b49e50afe124880abbfeccdc) refactor: use ES6 shorthand property syntax for backupCodes - [`56cd0c6`](https://github.com/better-auth/better-auth/commit/56cd0c69e106fd68fdb5d64cd32cf0e29fa7c351) fix(two-factor): handle all storage modes in viewBackupCodes - [`15e01da`](https://github.com/better-auth/better-auth/commit/15e01dac08e8796676e1f4f19aac68dd49d61b25) test(two-factor): add viewBackupCodes regression tests - [`0a00273`](https://github.com/better-auth/better-auth/commit/0a002734debd21dddb304a6f100950680875d79f) Merge remote-tracking branch 'upstream/canary' into fix/backup-codes-json-parse-error ### 📊 Changes **2 files changed** (+84 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/two-factor/backup-codes/index.ts` (+6 -6) 📝 `packages/better-auth/src/plugins/two-factor/two-factor.test.ts` (+78 -0) </details> ### 📄 Description Fixes #5168 ### Problem When calling `auth.api.viewBackupCodes()` after generating new backup codes, users get a JSON parse error: ``` SyntaxError: Unexpected number in JSON at position 1 ``` The `viewBackupCodes` endpoint was missing the `decryptBackupCodes()` call, causing it to fail when storage encryption is enabled. ### Solution Added proper dual-layer decryption to handle all storage configurations: - `decryptBackupCodes()` removes optional storage encryption layer - `getBackupCodes()` removes internal encryption and parses JSON This matches the pattern used in the `verifyBackupCode` endpoint. ### Tests - [x] All 17 two-factor tests pass - [x] Handles plain/undefined storage mode - [x] Handles encrypted storage mode - [x] Handles custom encryption functions --- <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-15 21:14:19 -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#22726