How to fix auth/invalid-email in Firebase
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.
Quick fix
Fixes auth/invalid-email
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');
}
}- Trim and validate email input before authentication
- Check that you're passing string values, not objects
- Implement proper error handling for invalid email
- Add client-side email validation before Firebase calls
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');}}auth/invalid-email
What this error means
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.
Typical symptoms
- Authentication fails with error code auth/invalid-email
- Login attempts rejected when email appears valid
- User registration fails silently or shows generic validation error
- Email validation works locally but fails in production
- Whitespace appears in email fields without user awareness
Common causes
- Email address missing the @ symbol (e.g., "userexample.com")
- Email contains invalid special characters or spaces
- Whitespace characters (spaces, tabs, newlines) prepended or appended to email
- Passing an object instead of string value to authentication method
- Email input not trimmed before sending to Firebase
- Email input in uppercase but Firebase rejects certain formats
How to fix "auth/invalid-email"
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: 'user@example.com' } };
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., user@example.com)');
} 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;
}Advanced notes
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.
Frequently asked questions
- What does "auth/invalid-email" mean?
- 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.
- How do I fix "auth/invalid-email"?
- 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.
- What causes "auth/invalid-email"?
- Email address missing the @ symbol (e.g., "userexample.com") Email contains invalid special characters or spaces Whitespace characters (spaces, tabs, newlines) prepended or appended to email
- Is the "auth/invalid-email" error serious?
- It is a low-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐Firebase Official Documentation
- ๐firebase.google.com - errors(Official Docs)
- ๐firebase.google.com - password auth(Official Docs)
- ๐firebase.google.com - errors(Official Docs)
- ๐github.com - 5164(GitHub)
- ๐