gpt4 book ai didi

android - 如何在 Firebase 中创建用户时获取用户 ID

转载 作者:行者123 更新时间:2023-12-05 00:21:36 25 4
gpt4 key购买 nike

在我的项目中,只有管理员可以将用户添加到 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结构如下。我想在创建用户时获取用户 ID
Here is my Firebase Auth Image

And want to set id here

最佳答案

当您在 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();
}

关于android - 如何在 Firebase 中创建用户时获取用户 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44896522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com