【问题标题】:How can I get user id at the time of user creation in Firebase如何在 Firebase 中创建用户时获取用户 ID
【发布时间】:2017-07-04 03:25:27
【问题描述】:

在我的项目中,只有管理员可以将用户添加到 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

【问题讨论】:

    标签: android firebase-realtime-database firebase-authentication


    【解决方案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();
        }
    

    【讨论】:

    • 谢谢阿洛特兄弟
    • @AtifRizwan 欢迎!
    • 不是返回管理员的id而不是创建的用户的id吗?
    【解决方案2】:

    @Himashu Nain 对这个问题的回答不正确。 firebaseAuth.getCurrentUser().getUid(); 将获取 Admin 的 userId,因为 Admin 已登录并且是当前用户。 @AtifRizwan 想要创建员工帐户的用户 ID(不是管理员的!)。要获取创建帐户的用户 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());
                    }
                }
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 2023-01-20
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      • 2020-08-13
      • 2020-02-28
      相关资源
      最近更新 更多