How to check if user exists with email (Firebase +) - SOLVED
Actions & Logic
I've spent the last 2-3 years looking for an answer on how to do this in Flutterflow as there's no set way. Even with the help from AI, I've typically run into issues running a cloud function to check if user exists as you cannot call admin.initializeApp() in Flutterflow Cloud Functions Editor.
What I used to try: (which doesn't work)
Writing Cloud Function with input parameter as email
Returning Boolean value
What have you tried so far?
What FINALLY worked: (see below)
const functions = require('firebase-functions');
const admin = require('firebase-admin');
exports.checkEmailExists = functions.https.onCall(async (data, context) => {
const email = data.email;
// 1. Validate Input
if (!email || typeof email !== 'string') {
throw new functions.https.HttpsError(
'invalid-argument',
'The function must be called with a valid email string.'
);
}
try {
// 2. Use Firebase Admin SDK to check if user exists by email
// This will throw an error if the user does NOT exist.
await admin.auth().getUserByEmail(email); // We don't need the userRecord object here, just if it succeeds or fails
// If we reach here, it means getUserByEmail succeeded, so the user exists
return { exists: true }; // Just return the boolean status
} catch (error) {
// 3. Handle specific 'auth/user-not-found' error
if (error.code === 'auth/user-not-found') {
return { exists: false }; // User does not exist
}
// 4. Re-throw other unexpected errors
console.error("Error in checkEmailExists Cloud Function:", error);
throw new functions.https.HttpsError(
'unknown',
'An unexpected error occurred while checking email existence.',
error.message
);
}
});
TIPS:
Return a JSON object with $.exist = trueOR$.exist = false
Use the JSON object to either update page/component state after calling custom function
Use the JSON object value to filter based on condition
You can return other information like display_name or uid if you wish to do later on
Did you check FlutterFlow's Documentation for this topic?