在我的项目中,只有管理员可以将用户添加到firebase。 在创建时,我想获得用户ID,因为我想在Firebase数据库中设置该用户的个人资料。
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();
}
}
});
}
我的火力架结构是 我的火力架结构如下。我希望在用户创建时获得用户ID Here is my Firebase Auth Image
答案 0 :(得分:1)
当您在firebase auth中创建用户时,它会自动登录。因此,您可以在onComplete()中获取您的ID,如果方法为:
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();
}
答案 1 :(得分:0)
@Himashu Nain对这个问题的回答不正确。 firebaseAuth.getCurrentUser().getUid();
将获得管理员的userId,因为该管理员已登录并且是当前用户。
@AtifRizwan希望创建员工帐户的userId(而不是管理员的!)。要获取所创建帐户的用户ID,您可以从完成监听器返回的任务中获取用户信息:即
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());
}
}
});