Home > database >  Failed to login after change the email or password of the user
Failed to login after change the email or password of the user

Time:04-25

In my project, I have users and their information stores in Firebase. After they register I send their data to Firebase like this:

  Map<String, dynamic> toMap() {
    return {
      'name': name,
      'id': id,
      'email': email,
      'password': password,
      'age': age,
      'status': status,
    };
  }

The admin can modify their account info and after apply the modification he submit it and data stored in Firebase like this:

 final querySnapshot = await FirebaseFirestore.instance
                              .collection('UsersInformation')
                              .get();

                          Map<String, dynamic> userData = {};
                          if (user.name != null) {
                            userData['name'] = user.name;
                          }
                          if (user.id != null) {
                            userData['id'] = user.id;
                          }
                          if (user.email != null) {
                            userData['email'] = user.email;
                          }
                          if (user.password != null) {
                            userData['password'] = user.password;
                          }
                          if (age.zones != null) {
                            userData['zones'] = age.zones;
                          }
                          if (user.status != null) {
                            userData['status'] = user.status;
                          }

                          await FirebaseFirestore.instance
                              .collection('UsersInformation')
                              .doc(querySnapshot.docs[index].id)
                              .update(userData);

The problem is when the admin changes the user's email or password and then the user tries to log in with the new email, message appears that the email doesn't exist!! while it exists or if the admin changes the password for the user, and then the user tries to log in with the new password message appears that the password is wrong!

what I'm doing wrong here?

CodePudding user response:

Firestore and Authentication are two different areas of Firebase. Updating your database's data doesn't change your authentication data. You need to use the FirebaseAuth module with functions like:

await user?.updateEmail("[email protected]");
await user?.updatePassword(newPassword);

You can find the associated documentation for Flutter at this page:

Manage Users in Firebase

CodePudding user response:

I think what's happening here is when you update the password, you only send the password data back and on receiving this data, firebase replaces all data of the user with only this new password. I think that's why the error user email doesn't exist comes out and also why the password shows as wrong because the user doesn't exist in the first place.

  • Related