How to fix auth/invalid-verification-code in Firebase

FirebaseBEGINNERMEDIUMUpdated Dec 22, 2025ยท 288 views

This error occurs when a user provides an incorrect SMS verification code during Firebase phone authentication. The code may be wrong, expired, or the verification ID may be invalid.

Quick fix

Fixes INVALID_VERIFICATION_CODE

# No code needed - this is a Firebase Console setting
  1. Verify the phone authentication provider is enabled
  2. Implement proper error handling with try-catch blocks
  3. Ask the user to re-enter the verification code
  4. Implement code expiry handling and resend functionality
firebase โ€” error
# No code needed - this is a Firebase Console setting
auth/invalid-verification-code

What this error means

The "auth/invalid-verification-code" error is thrown by Firebase Authentication when attempting to sign in with phone number authentication and the provided SMS verification code does not match the code sent to the user. This is a client-side validation error that occurs after the user receives an SMS message with a verification code and enters it into your app. Firebase checks the code against the verification ID that was generated when the sign-in process started. If they do not match, or if the code has expired, this error is raised. The error can manifest differently across platforms (Android, iOS, Flutter, Web) but the underlying cause is the same: the verification credentials are invalid.

Typical symptoms

  • Users receive "The sms verification code used to create the phone auth credential is invalid" message
  • Phone authentication fails immediately after entering the SMS code
  • Error appears only in production but not during local debugging
  • Sign-in fails for certain users or phone numbers
  • Verification code prompt appears multiple times for the same sign-in attempt

Common causes

  1. User entered the verification code incorrectly (typo or misread)
  2. Verification code has expired (typically 5-10 minutes)
  3. Verification ID is invalid or was not properly stored/passed between requests
  4. Mismatch between the phone number used to request the code and the phone number being verified
  5. Firebase phone authentication provider is disabled in the Firebase Console
  6. The verification code was already used in a previous sign-in attempt
  7. App was force-closed or crashed between requesting and confirming the code

How to fix "auth/invalid-verification-code"

1Verify the phone authentication provider is enabled

Go to Firebase Console > Authentication > Sign-in method tab and ensure "Phone" is enabled. If it is not, enable it and save your changes. Without this enabled, all phone authentication attempts will fail.

bash
# No code needed - this is a Firebase Console setting
2Implement proper error handling with try-catch blocks

Wrap your phone authentication code in try-catch blocks to catch the invalid-verification-code error specifically. This allows you to display a helpful message and offer to resend the code.

javascript
// Web/JavaScript example
const confirmationResult = await signInWithPhoneNumber(auth, phoneNumber, appVerifier);
const code = prompt("Enter verification code");

try {
  const result = await confirmationResult.confirm(code);
  console.log("User signed in successfully");
} catch (error) {
  if (error.code === "auth/invalid-verification-code") {
    console.error("Invalid verification code. Please try again or request a new code.");
    // Show UI to let user re-enter or resend
  } else {
    console.error("Sign-in failed:", error.message);
  }
}
dart
// Flutter example
try {
  UserCredential userCredential = await FirebaseAuth.instance.signInWithCredential(
    PhoneAuthProvider.credential(
      verificationId: verificationId,
      smsCode: smsCode,
    ),
  );
  print("User signed in successfully");
} on FirebaseAuthException catch (e) {
  if (e.code == 'invalid-verification-code') {
    print("Invalid verification code. Please check and try again.");
    // Show error message to user
  } else {
    print("Sign-in failed: ${e.message}");
  }
}
kotlin
// Android (Kotlin) example
credential = PhoneAuthProvider.getCredential(verificationId, code)
try {
  mAuth.signInWithCredential(credential)
    .addOnCompleteListener(this) { task ->
      if (task.isSuccessful) {
        Log.d("PhoneAuth", "User signed in successfully")
      } else {
        val e = task.exception
        if (e is FirebaseAuthInvalidCredentialsException) {
          Log.w("PhoneAuth", "Invalid verification code")
          // Show error message to user
        }
      }
    }
} catch (e: Exception) {
  Log.e("PhoneAuth", "Sign-in failed", e)
}
3Ask the user to re-enter the verification code

Provide a UI element that allows users to input the verification code again. Some users may have mistyped or autocorrect may have changed the code. Display the error message clearly and give them the ability to try again without restarting the entire sign-in process.

javascript
// React example
const [code, setCode] = useState("");
const [error, setError] = useState("");

const handleConfirm = async () => {
  try {
    await confirmationResult.confirm(code);
    // Success - navigate to next screen
  } catch (err) {
    if (err.code === "auth/invalid-verification-code") {
      setError("The code you entered is incorrect. Please try again.");
      setCode(""); // Clear the input
    }
  }
};

return (
  <div>
    <input
      type="text"
      value={code}
      onChange={(e) => setCode(e.target.value)}
      placeholder="Enter 6-digit code"
    />
    {error && <div style={{ color: "red" }}>{error}</div>}
    <button onClick={handleConfirm}>Verify Code</button>
  </div>
);
4Implement code expiry handling and resend functionality

Verification codes typically expire after 5-10 minutes. Implement a timer that shows users how much time remains, and provide a "Resend Code" button. When resending, you must request a new verification ID.

javascript
// Web example with resend logic
let verificationId = null;
let confirmationResult = null;

const sendCode = async (phoneNumber) => {
  const appVerifier = window.recaptchaVerifier;
  try {
    confirmationResult = await signInWithPhoneNumber(auth, phoneNumber, appVerifier);
    console.log("Code sent to", phoneNumber);
    // Start 5-minute timer
    setTimeout(() => {
      console.log("Code expired. User must resend.");
    }, 300000);
  } catch (error) {
    console.error("Failed to send code:", error.message);
  }
};

const resendCode = async (phoneNumber) => {
  // Clear previous verificationId and request a new one
  confirmationResult = null;
  await sendCode(phoneNumber);
};
dart
// Flutter example
Final int codeExpiresSecs = 300; // 5 minutes
Final int codeResendSecs = 30; // Can resend after 30 seconds

await FirebaseAuth.instance.verifyPhoneNumber(
  phoneNumber: phoneNumber,
  timeout: Duration(seconds: codeExpiresSecs),
  verificationCompleted: (credential) {
    // Auto-complete if available
  },
  verificationFailed: (e) {
    print("Verification failed: ${e.message}");
  },
  codeSent: (verificationId, resendToken) {
    // Show input form with timer
    // Enable resend button after 30 seconds
  },
  codeAutoRetrievalTimeout: (verificationId) {
    // Code expired
  },
);
5Store and manage the verification ID correctly

The verification ID must be stored securely and persisted for the entire sign-in flow. If your app crashes, is backgrounded, or the user navigates away, you may lose the verification ID, causing subsequent confirmation attempts to fail.

javascript
// Store verificationId in state during the sign-in flow
const [verificationId, setVerificationId] = useState(null);

const sendCode = async (phoneNumber) => {
  const appVerifier = window.recaptchaVerifier;
  try {
    const confirmationResult = await signInWithPhoneNumber(
      auth,
      phoneNumber,
      appVerifier
    );
    // Store the verification ID
    setVerificationId(confirmationResult.verificationId);
  } catch (error) {
    console.error("Failed:", error);
  }
};

const confirmCode = async (code) => {
  try {
    const credential = PhoneAuthProvider.credential(
      verificationId, // Use stored ID
      code
    );
    await signInWithCredential(auth, credential);
  } catch (error) {
    if (error.code === "auth/invalid-verification-code") {
      console.error("Invalid code");
    }
  }
};
6Use Firebase test phone numbers for development

During development and testing, use Firebase test phone numbers instead of real numbers. This prevents quota issues and allows you to use predetermined verification codes.

1. Go to Firebase Console > Authentication > Phone > Phone numbers for testing
2. Click "Add phone number for testing"
3. Enter a test phone number (e.g., +1 555-0100)
4. Enter a test code (e.g., 123456)
5. Use this phone number and code combination in your app during testing

javascript
// No code change needed - just use the test phone number
const testPhoneNumber = "+1 555-0100"; // From Firebase Console
const testCode = "123456"; // From Firebase Console

// This will succeed immediately without sending an SMS
await signInWithPhoneNumber(auth, testPhoneNumber, appVerifier);
await confirmationResult.confirm(testCode); // Will succeed

Advanced notes

Cross-Platform Considerations: This error manifests as "PlatformException(ERROR_INVALID_VERIFICATION_CODE)" in Flutter and "FirebaseAuthInvalidCredentialsException" in Android native code. Always check the platform-specific error type in your error handler.

Production Issues: Some developers report that this error does not occur in debug mode but surfaces consistently in production builds (TestFlight, Play Store). This is typically caused by missing SHA fingerprints. Ensure your app's debug and production SHA-1 and SHA-256 fingerprints are registered in the Firebase Console under Project Settings > Your Apps > App Signing Certificate SHA-1/SHA-256.

SMS Quota Limits: Firebase enforces limits on SMS messages sent to a single phone number within a time period. If you exceed these limits, phone verification may be throttled. For development, use test phone numbers to avoid consuming quota.

ReCAPTCHA Verification: On web, phone authentication uses reCAPTCHA by default. Ensure the reCAPTCHA verifier is initialized and visible to the user before sending the code. Errors in reCAPTCHA setup can appear as invalid code errors.

Session and Timeout: The verification ID is session-specific. If your app is backgrounded for too long or the device's clock is significantly off, verification may fail. This is a security measure to prevent code reuse.

Frequently asked questions

What does "INVALID_VERIFICATION_CODE" mean?
The "auth/invalid-verification-code" error is thrown by Firebase Authentication when attempting to sign in with phone number authentication and the provided SMS verification code does not match the code sent to the user. This is a client-side validation error that occurs after the user receives an SMS message with a verification code and enters it into your app. Firebase checks the code against the verification ID that was generated when the sign-in process started.
How do I fix "INVALID_VERIFICATION_CODE"?
This error occurs when a user provides an incorrect SMS verification code during Firebase phone authentication. The code may be wrong, expired, or the verification ID may be invalid.
What causes "INVALID_VERIFICATION_CODE"?
User entered the verification code incorrectly (typo or misread) Verification code has expired (typically 5-10 minutes) Verification ID is invalid or was not properly stored/passed between requests
Is the "INVALID_VERIFICATION_CODE" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading