xxxxxxxxxx
const emailExists = (email) => {
// Assuming you have already initialized Firebase and have a reference to the Firebase database
return new Promise((resolve, reject) => {
const emailRef = firebase.database().ref('users').orderByChild('email').equalTo(email);
// Listen for value changes in the emailRef
emailRef.on('value', (snapshot) => {
if (snapshot.exists()) {
resolve(true); // Email exists in the database
} else {
resolve(false); // Email does not exist in the database
}
}, (error) => {
reject(error); // Error occurred while checking email existence
});
});
};
// Usage:
emailExists('example@example.com')
.then((exists) => {
console.log(exists); // true if email exists, false otherwise
})
.catch((error) => {
console.error(error); // Handle error
});
xxxxxxxxxx
//check email already exist or not.
firebaseAuth.fetchSignInMethodsForEmail(email)
.addOnCompleteListener(new OnCompleteListener<SignInMethodQueryResult>() {
@Override
public void onComplete(@NonNull Task<SignInMethodQueryResult> task) {
boolean isNewUser = task.getResult().getSignInMethods().isEmpty();
if (isNewUser) {
Log.e("TAG", "Is New User!");
} else {
Log.e("TAG", "Is Old User!");
}
}
});
xxxxxxxxxx
import { initializeApp } from "firebase/app";
import { getAuth, fetchSignInMethodsForEmail } from "firebase/auth";
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_API_KEY,
authDomain: process.env.NEXT_PUBLIC_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_APP_ID,
}
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
async function check(email) {
try {
const signInMethods = await fetchSignInMethodsForEmail(auth, email);
console.log(signInMethods);
if (signInMethods.length === 0) {
return false;
//doesn't exist
} else {
return true;
//exists
}
} catch (error) {
console.error("Error checking email existence:", error);
// You might want to handle the error in a specific way or throw it again.
throw error;
}
}