skip to Main Content

I am trying to create a new user in Firebase. I am using the auth.createUserWithEmailAndPassword method but it does not create a new entry in the firebase db.

Here is my code:

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

if (newUser != null) {
  Navigator.of(context).pushReplacement(
    MaterialPageRoute(builder: (context) => const VerifyEmailScreen()),
  );
} else {
  setState(() {
    registrationFail = true;
  });
}

I get an error message "An undefined error happened" which is the default error message in the catch code and then it just hangs.

I’m not sure I am communicating with Firebase. How do I check to see if I am talking to the db?

I have searched for a solution but everything is very old and nothing works.

Any help would be greatly appreciated.
Thanks

2

Answers


  1. The createUserWithEmailAndPassword API exists on Firebase Authentication, not in Cloud Firestore. So it creates a new record for the user in Firebase Authentication, but won’t create anything in Firestore.

    If you want to write to Firestore, check its API docs here.

    If you’re getting an error message that is vague, it might be that email enumeration protection is enabled on your project – which intentionally hides a lot of detail from API responses to prevent against attacks. To see if this is the case, you may want to temporarily disable the protection and see if you get a clearer error message.

    Login or Signup to reply.
  2. To troubleshoot this issue, you can re-check the following setup carefully.

    1. Ensure that you have correctly placed the google-services.json file within your project directory.
    PROJECT_ROOT_FOLDER > android > app > google-services.json
    
    1. Ensure that you’ve initialized the Firebase configuration in your main.dart.
    Future<void> main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp(); // Initialize firebase here
      
      runApp(
        MaterialApp(
          home: YourAppHomePage(), // Replace with your app's home page
        ),
      );
    }
    
    1. Handle error with try-catch:
    try {
      final newUser = await _auth.createUserWithEmailAndPassword(
        email: email,
        password: password,
      );
    
      if (newUser != null) {
        Navigator.of(context).pushReplacement(
          MaterialPageRoute(builder: (context) => const VerifyEmailScreen()),
        );
      } else {
        setState(() {
          registrationFail = true;
        });
      }
    } catch (e) {
      // Handle the specific error
      print("Error creating user: $e");
      setState(() {
        registrationFail = true;
      });
    }
    

    It helps to understand the issue.

    1. Additional: Not necessary in the Authentication case but please ensure Firestore Database or Cloud Firestore rules are not preventing the write operation. It should be like:
    rules_version = '2';
    
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if true;
        }
      }
    }
    

    You can find your entry here:
    Authentication Dashboard

    Hope your code will run as you expected.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search