Skip to content

Commit f691d5e

Browse files
committed
fix: change invalid backup code status
1 parent 9cddaad commit f691d5e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

packages/better-auth/src/plugins/two-factor/backup-codes/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export const backupCode2fa = (
132132
ctx.context.secret,
133133
);
134134
if (!validate.status) {
135-
throw new APIError("BAD_REQUEST", {
135+
throw new APIError("UNAUTHORIZED", {
136136
message: "Invalid backup code",
137137
});
138138
}

packages/better-auth/src/plugins/two-factor/two-factor.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,21 @@ describe("two factor", async () => {
218218
});
219219
expect(currentBackupCodes.backupCodes).toBeDefined();
220220
expect(currentBackupCodes.backupCodes).not.toContain(backupCode);
221+
222+
const res = await client.twoFactor.verifyBackupCode({
223+
code: "invalid-code",
224+
fetchOptions: {
225+
headers,
226+
onSuccess(context) {
227+
const parsed = parseSetCookieHeader(
228+
context.response.headers.get("Set-Cookie") || "",
229+
);
230+
const token = parsed.get("better-auth.session_token")?.value;
231+
expect(token?.length).toBeGreaterThan(0);
232+
},
233+
},
234+
});
235+
expect(res.error?.message).toBe("Invalid backup code");
221236
});
222237

223238
it("should trust device", async () => {

0 commit comments

Comments
 (0)