The auth/invalid-email error occurs when an email address is not properly formatted or contains invalid characters. Fix it by validating and trimming email input before passing it to Firebase authentication methods.
Firebase throws this error when you attempt to authenticate using an email address that does not conform to the standard email format. This is a client-side validation error that Firebase enforces to ensure only valid email addresses can be used for authentication. The error is triggered by the Firebase SDK when creating a user account, signing in, or performing password resets with a malformed email address.
The most common cause is whitespace in the email field. Always clean user input before sending to Firebase:
JavaScript/Web SDK:
const email = userInput.trim().toLowerCase();
try {
await firebase.auth().signInWithEmailAndPassword(email, password);
} catch (error) {
if (error.code === 'auth/invalid-email') {
console.error('Invalid email format');
}
}TypeScript with Firebase Admin SDK:
const email = userInput.trim().toLowerCase();
try {
const user = await admin.auth().getUserByEmail(email);
} catch (error) {
if (error.code === 'auth-user-not-found') {
// Different error when using Admin SDK
}
}iOS (Swift):
let email = emailInput.trimmingCharacters(in: .whitespaces).lowercased()
Auth.auth().signIn(withEmail: email, password: password) { result, error in
if let error = error as? NSError {
if error.code == AuthErrorCode.invalidEmail.rawValue {
print('Invalid email format')
}
}
}Android (Kotlin):
val email = emailInput.trim().toLowerCase()
FirebaseAuth.getInstance().signInWithEmailAndPassword(email, password).addOnFailureListener { exception ->
if (exception is FirebaseAuthInvalidUserException) {
Log.e('Auth', 'Invalid email format')
}
}A common mistake when managing state is passing the entire state object instead of the string value:
WRONG - passing the entire email object:
const state = { email: { value: '[email protected]' } };
await firebase.auth().signInWithEmailAndPassword(state.email, password);
// Error: auth/invalid-emailCORRECT - passing only the string value:
await firebase.auth().signInWithEmailAndPassword(state.email.value, password);WRONG with React hooks:
const [email, setEmail] = useState({});
await firebase.auth().signInWithEmailAndPassword(email, password);CORRECT with React hooks:
const [email, setEmail] = useState('');
await firebase.auth().signInWithEmailAndPassword(email, password);Add specific error handling to distinguish invalid email from other authentication errors:
async function handleSignIn(email, password) {
try {
const email = email.trim().toLowerCase();
await firebase.auth().signInWithEmailAndPassword(email, password);
} catch (error) {
if (error.code === 'auth/invalid-email') {
// Email format is wrong
showError('Please enter a valid email address (e.g., [email protected])');
} else if (error.code === 'auth/user-not-found') {
// Email format is valid but account doesn't exist
showError('No account found with this email');
} else if (error.code === 'auth/wrong-password') {
// User exists but password is incorrect
showError('Incorrect password');
} else {
showError('Authentication failed: ' + error.message);
}
}
}Validate email format locally before making Firebase requests to provide faster feedback:
Simple regex pattern for email validation:
function isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
async function signInUser(emailInput, password) {
const email = emailInput.trim().toLowerCase();
// Client-side validation first
if (!isValidEmail(email)) {
showError('Invalid email format');
return;
}
try {
await firebase.auth().signInWithEmailAndPassword(email, password);
} catch (error) {
handleAuthError(error);
}
}Using a library like email-validator:
import validator from 'email-validator';
const email = userInput.trim().toLowerCase();
if (!validator.validate(email)) {
showError('Invalid email format');
return;
}Firebase has different validation rules between SDKs. When creating a user with the Admin SDK, invalid email might be accepted at account creation but rejected during sign-in, creating an inconsistency. Some special characters (like accented letters) may pass client validation but fail server validation. For maximum compatibility, stick to standard ASCII email formats. If you're using custom claims or linking providers, ensure the email is validated before updating user metadata. React Native Firebase and Flutter SDKs expose errors via FirebaseAuthException with the error.code property - use try-catch blocks with on FirebaseAuthException to handle this gracefully.
Callable Functions: INTERNAL - Unhandled exception
How to fix "Callable Functions: INTERNAL - Unhandled exception" in Firebase
messaging/UNSPECIFIED_ERROR: No additional information available
How to fix "messaging/UNSPECIFIED_ERROR: No additional information available" in Firebase Cloud Messaging
auth/invalid-hash-algorithm: Hash algorithm doesn't match supported options
How to fix "auth/invalid-hash-algorithm: Hash algorithm doesn't match supported options" in Firebase
App Check: reCAPTCHA Score Too Low
App Check reCAPTCHA Score Too Low
storage/invalid-url: Invalid URL format for Cloud Storage reference
How to fix invalid URL format in Firebase Cloud Storage