how to check if a user already registered in firestore?

Viewed 1086

I am working on a flutter app that has registration screen. I have implemented the email auth and want to check if the user already registered or not? I have used:

final newUser = await _auth.createUserWithEmailAndPassword(email: email.trim(),password: password);

to register the user. if the user has already registered, it throw an error that days the email is already in use, but what i want is to customize the message. so, how to retrieve the a specific user data (e.g email..) from firebase collection by giving an specific email? for example: before registration call, want to query from collection if the specified email is already in use or not?

i have tried the fetchSignInMethodsForEmail as well, but could not get any result.

1 Answers

Solved!

Future<void> isEmailRegistered(String email) async {
final QuerySnapshot result = await Firestore.instance
    .collection('users')
    .where('userEmail', isEqualTo: email)
    .limit(1)
    .getDocuments();
final List<DocumentSnapshot> documents = result.documents;
if (documents.length > 0)
  print('Email is in Use');
else
  print('Email is not in Use');

}

Related