【问题标题】:Connecting Real time database to Authentication database on Firebase将实时数据库连接到 Firebase 上的身份验证数据库
【发布时间】:2020-03-10 16:41:57
【问题描述】:

我正在创建一个允许用户登录和注册的应用程序。目前我正在使用 firebase 来存储这些数据,但我需要的数据不仅仅是电子邮件和密码。我可以将多个字段存储到实时数据库中,但是我的登录页面使用的是带有 Firebase 的身份验证。有没有办法把两者联系起来?

登录页面

 //creating a new user
        firebaseAuth.signInWithEmailAndPassword(username, password)
                .addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(MainActivity.this, "Login Successful", Toast.LENGTH_LONG).show();
                            startActivity(new Intent(getApplicationContext(), FAQ.class));
                            finish();
                        } else{
                            Toast.makeText(MainActivity.this, "Login Error", Toast.LENGTH_LONG).show();
                        }
                    }
                });

    }

注册页面

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //firebaseAuth =FirebaseAuth.getInstance();
        userN=(EditText)findViewById(R.id.userN);
        password2=(EditText)findViewById(R.id.password2);

        gender2=(EditText)findViewById(R.id.gender2);
        headInjury2=(EditText)findViewById(R.id.headInjury2);
        Smoker2=(EditText)findViewById(R.id.Smoker2);
        dateOfBirth2=(EditText)findViewById(R.id.dateOfBirth2);
        submitBtn2=(Button)findViewById(R.id.submitBtn2);
        user= new User();
        reff= FirebaseDatabase.getInstance().getReference().child("User");

        submitBtn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int date=Integer.parseInt(dateOfBirth2.getText().toString().trim());


                user.setUserN(userN.getText().toString().trim());
                user.setPassword2(password2.getText().toString().trim());
                user.setGender2(gender2.getText().toString().trim());
                user.setHeadInjury2(headInjury2.getText().toString().trim());
                user.setSmoker2(Smoker2.getText().toString().trim());
                user.setDOB(date);
                reff.push().setValue(user);
                Toast.makeText(Register.this, "Successfully registered", Toast.LENGTH_LONG).show();


            }
        });
    }

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    要在实时数据库中存储有关用户的其他数据,您通常需要将该信息存储在用户的 UID 下。

    所以你会有类似的东西

    Users: {
      uidOfCaitlin: {
        firstName: "Caitlin",
        lastName: "Daly McDowell",
        dob: "2020-03-10"
      },
      uidOfFrank: {
        firstName: "Frank",
        lastName: "van Puffelen",
        dob: "1970-03-10"
      }
    }
    

    要像这样存储数据,您可以执行以下操作:

    DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference().child("Users");
    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    
    usersRef.child(uid).setValue(user);
    

    这个话题之前已经讨论过很多了,所以我也建议你看看:

    【讨论】:

    • 谢谢! :) 无论如何使用实时数据库将用户登录到应用程序?
    • @CaitlinDalyMcDowell 登录是您使用 Firebase 身份验证 API 执行的操作。用户登录后,您可以在客户端代码中在数据结构中使用他们的 UID,并且可以在数据库的服务器端安全规则中安全地使用他们的 UID。请参阅我在回答中提供的链接,因为它们提供了更多示例。
    猜你喜欢
    • 2019-06-28
    • 2017-12-26
    • 2023-03-20
    • 1970-01-01
    • 2019-05-05
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多