![](/img/trans.png)
[英]How can I get User object from its user id in Firebase Android
[英]How can I get user id at the time of user creation in Firebase
在我的項目中,只有管理員可以將用戶添加到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();
}
}
});
}
我的firebase結構是,而我的firebase結構如下。 我想在創建用戶時獲取用戶ID, 這是我的Firebase身份驗證圖像
當您在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();
}
@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());
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.