skip to Main Content

I am trying to do authentication using createUserWithEmailAndPassword .The user iis getting created since I can signin using the email and password which created using createUserWithEmailAndPassword .But my firestore database is not showing the document which I have created using createUserWithEmailAndPassword().

This is my code:

onPressed: () async {
                    try {
                      final newuser = await FirebaseAuth.instance
                          .createUserWithEmailAndPassword(
                        email: email ?? 'error',
                        password: password ?? 'error',
                      );
                    
                      if (newuser != null) {
                        Navigator.push(
                          context,
                          MaterialPageRoute(builder: (context) => home()),
                        );
                      }
                    } catch (e) {
                      print(e);
                    }

I have created 3 users with this method and I’m able to login with these credential but these users are not showing in my Firestore Database.
database

3

Answers


  1. To store the data in the database you need to use a Firestore instance to store data in firebase Firestore.

    onPressed: () async {
        try {
          final newuser = await FirebaseAuth.instance
              .createUserWithEmailAndPassword(
            email: email ?? 'error',
            password: password ?? 'error',
          );
          await FirebaseFirestore.instance.collection('Users').doc(newuser!.uid).set({
            email: newuser.email
          });
          if (newuser != null) {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => home()),
            );
          }
        } catch (e) {
          print(e);
        }
    }
    

    You need to put the data which you want to store in firestore in set() function.

    Login or Signup to reply.
  2. Firebase Authentication data are show into the login into firebase then on tap of Authentication options menu -> user section , you can view store 3 users data in this section

    Login or Signup to reply.
  3. As @ashish already mentioned, you need to store all your extra parameters in the firestore as documents. A better way to implement this is to use different function to register user and create a user document related to that registered user.

    Below is an example I created to help out!

    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:firebase_auth/firebase_auth.dart';
    import 'package:flutter/material.dart';
    
    void main() {
      String email = "[email protected]";
      String password = "123456";
      String firstName = "test";
      String lastName = "test";
      String phoneNumber = "09876543456";
    
      /// This is the function to create new user
      Future<void> createUser(
        email,
        password,
        firstName,
        lastName,
        phoneNumber,
        /*... and any other params you wish to collect */
      ) async {
        // firebase auth
        final FirebaseAuth auth = FirebaseAuth.instance;
    
        // firestore db
        FirebaseFirestore db = FirebaseFirestore.instance;
    
        // try-catch creation process to accurate exception
        try {
          final credential = await auth.createUserWithEmailAndPassword(
            email: email,
            password: password,
          );
    
          // get user id
          String userID = credential.user!.uid;
    
          // creating user profile on db
          await db.collection("users").doc(userID).set({
            "id": userID,
            "firstName": firstName,
            "lastName": lastName,
            "phoneNumber": phoneNumber,
            // ... other params
          });
    
          print("Signed up successfully!");
        } on FirebaseAuthException catch (error) {
          print("Something is wrong: $error");
        }
      }
    
      /// this is the create button
      TextButton(
        onPressed: () async {
          await createUser(
            email,
            password,
            firstName,
            lastName,
            phoneNumber,
            /*... and any other params you wish to collect */
          );
        },
        child: const Text("Create test user"),
      );
    }
    
    

    Let me know if you need anything else :). BYE!!!

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