简体   繁体   中英

How can I get user id at the time of user creation in Firebase

In my project only Admin can add user to firebase. At the time of creation i want to get user id because i want to set up profile of that user in Firebase Database.

public void addNewFaculty(View view){
    String name,email,password,rePassword;
    name = txtname.getText().toString();
    email = txtEmail.getText().toString().trim();
    password = txtPassword.getText().toString().trim();
    rePassword = txtRepassword.getText().toString().trim();
    if(password.equals(rePassword))
    {
        Toast.makeText(this, "Password is not match", Toast.LENGTH_SHORT).show();
        return;
    }
    databaseReference= FirebaseDatabase.getInstance().getReference();
    firebaseUser = firebaseAuth.getCurrentUser();
    String id= firebaseUser.getUid();
    databaseReference = databaseReference.child("Profile").child(id);
    databaseReference.child("Name").setValue(name);
    // and some other things
    firebaseAuth.createUserWithEmailAndPassword(email,password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(task.isSuccessful()){
                        Toast.makeText(getApplicationContext(), "Employee Added Successfully", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(getApplicationContext(), "Please try Again", Toast.LENGTH_SHORT).show();
                    }
                }
            });

}

And my firebase structure is And my firebase structure is given below. I want to get user id at the time of user creation Here is my Firebase Auth Image

And want to set id here

When You create a user in firebase auth, it automatically log in. Therefore you can get your id in onComplete(), if method as:

    if(task.isSuccessful()){
       String id1 = firebaseAuth.getCurrentUser().getUid();
       //For adding into database

       databaseReference = databaseReference.child("Profile").child(id1);
       databaseReference.child("Name").setValue(name);

       Toast.makeText(getApplicationContext(), "Employee Added Successfully", Toast.LENGTH_SHORT).show();
    }else{
       Toast.makeText(getApplicationContext(), "Please try Again", Toast.LENGTH_SHORT).show();
    }

@Himashu Nain's answer is not correct for this question. firebaseAuth.getCurrentUser().getUid(); will get the Admin's userId since the Admin is logged in and is the current user. @AtifRizwan wants the userId of the employee account created(not the Admin's!). To get the user id of the account created, You can get the user information from the task returned by the completion listener: ie

 mAuth.createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());

                if (task.isSuccessful()) {
                    FirebaseUser user = task.getResult().getUser();

                    Log.d(TAG, "onComplete: uid=" + user.getUid());
                }
            }
        });

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