diff --git a/packages/better-auth/src/plugins/two-factor/otp/index.ts b/packages/better-auth/src/plugins/two-factor/otp/index.ts index e466a3ca7a..012ba8dfb6 100644 --- a/packages/better-auth/src/plugins/two-factor/otp/index.ts +++ b/packages/better-auth/src/plugins/two-factor/otp/index.ts @@ -255,9 +255,11 @@ export const otp2fa = (options?: OTPOptions) => { ); const [otp, counter] = toCheckOtp?.value?.split("!") ?? []; if (!toCheckOtp || toCheckOtp.expiresAt < new Date()) { - await ctx.context.internalAdapter.deleteVerificationValue( - `2fa-otp-${key}`, - ); + if (toCheckOtp) { + await ctx.context.internalAdapter.deleteVerificationValue( + toCheckOtp.id, + ); + } throw new APIError("BAD_REQUEST", { message: TWO_FACTOR_ERROR_CODES.OTP_HAS_EXPIRED, }); @@ -265,7 +267,7 @@ export const otp2fa = (options?: OTPOptions) => { const allowedAttempts = options?.allowedAttempts || 5; if (parseInt(counter) >= allowedAttempts) { await ctx.context.internalAdapter.deleteVerificationValue( - `2fa-otp-${key}`, + toCheckOtp.id, ); throw new APIError("BAD_REQUEST", { message: TWO_FACTOR_ERROR_CODES.TOO_MANY_ATTEMPTS_REQUEST_NEW_CODE,