简体   繁体   中英

After creating a user with email and password with Android Firebase I cannot save additional information in a users collection

After creating a user using the firebaseAuth function createUserWithEmailAndPassword (email, password) I am trying to save additional information of the users that are registered in a collection called "users". For that I use DocumentReference:

  mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if(task.isSuccessful()){
                FirebaseUser fuser = FirebaseAuth.getInstance().getCurrentUser();
                String idUser = fuser.getUid();
                DocumentReference documentReference = fStore.collection("users").document(fuser.getUid());
                Map<String, Object> user = new HashMap<>();
                Log.d("data", "username: "+username + " telef: "+telefono);
                user.put("username", username);
                user.put("telefono", telefono);
                         documentReference.set(user).addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                                Log.d(TAG, "User profile created");
                            }

                        }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Log.d(TAG, "can´t create profile: "+e.toString());
                            }
                        });
                //sign out the user.. dont work.. any ideas?
                FirebaseAuth.getInstance().signOut();
                mProgressBar.dismiss();
                //send the user to log in
                Intent intent = new Intent(RegisterActivity.this, loginActivity.class);
                //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                RegisterActivity.this.finish();

                Toast.makeText(getApplicationContext(), "Registered ok!", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getApplicationContext(), "can´t register", Toast.LENGTH_LONG).show();
            }
        }
    });

Apart from having instantiated:

        mAuth = FirebaseAuth.getInstance();
        fStore = FirebaseFirestore.getInstance();

I have also been forced to add the following:

private void initFirebase() {
        FirebaseApp.initializeApp(this);
        firebaseDatabase = FirebaseDatabase.getInstance();
        firebaseDatabase.setPersistenceEnabled(true);
        databaseReference = firebaseDatabase.getReference();
    }

Not even with the latter it works. Only the user with email and password is registered. The strange thing is that there is no error, it seems that the onSuccess or onFailure event does not occur. The database rules in the firebase console are:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

Basically this rule indicates that the user must be authenticated to be able to read / write to the database. My dependencies are:

implementation 'com.google.firebase:firebase-auth:19.3.0'
    implementation 'com.google.firebase:firebase-firestore:21.4.2'
    implementation 'com.google.firebase:firebase-storage:19.1.1'
    implementation 'com.google.firebase:firebase-database:18.0.1'

I should use firebase-firestore and firebase-auth but I have added firebase-database and firebase-storage in order to solve the problem but nothing happens.

Thanks in advance.

Your code is signing out the user before the database is written, so the write will fail any database rules that require the user to be signed in.

documentReference.set(user) is asynchronous and returns immediately, before any work has been done. The database will be written some time later. Meanwhile, your code goes on to immediately execute a signout with FirebaseAuth.getInstance().signOut() , which deletes the user's auth token, making the query happen without any credentials.

If you need the user to be signed out after writing the database, you should do that inside the database callback code. Though it's entirely clear to me why you would want that to happen. You should probably just remove the signout completely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM