【问题标题】:Firebase - How to make sure Authentication for an already authenticated user doesn't happen again?Firebase - 如何确保已通过身份验证的用户的身份验证不会再次发生?
【发布时间】:2017-05-12 19:21:36
【问题描述】:

我这几天一直在尝试解决这种情况,但我似乎找不到解决方案。所以这就是我想要做的: 我有一个应用程序,它使用firebase-ui-auth 通过电子邮件/Google 帐户对用户进行身份验证。一旦用户通过身份验证,我就会将用户的详细信息写入 Firebase 数据库子“clients”。现在我想确保下次同一用户登录时,他的详细信息不应该被写入“clients”。为此,我创建了另一个孩子“reg_clients”,我将之前登录过的用户的电子邮件 ID 推送到该孩子。 现在,由于 Firebase 无法在服务器端搜索 currentUser.getEmail() 是否已存在于“reg_clients”中,因此我使用:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == RC_SIGN_IN) {
            if (resultCode == RESULT_OK) {
                // user is signed in
                clientCompletedRegistrationReference.addListenerForSingleValueEvent(clientCompletedRegistrationValueEventListener);

                Log.d("in onActivityResult","user is:"+user.getDisplayName());
                Log.d("in onActivityResult","emailAddressList is:"+ Arrays.toString(emailAddressList.toArray()));
                Log.d("in onActivityResult","user.getEmail() is"+ emailAddressList.contains(user.getEmail()));

                if(emailAddressList.contains(user.getEmail())) {
                    Toast.makeText(NavDrawerActivity.this, "You're signed in again", Toast.LENGTH_SHORT).show();
                } else {
                    // Gets photoUrl when Google+ photo is set, otherwise returns null. Appending '?sz=100' at end gets a photo of 100x100 px
                    String photoUrl = user.getPhotoUrl() != null ? user.getPhotoUrl().toString()+"?sz=100": null;
                    Toast.makeText(NavDrawerActivity.this, "You're signed in for the first time", Toast.LENGTH_SHORT).show();

                    Client client = new Client(user.getDisplayName(),0,photoUrl,user.getDisplayName().replaceAll("\\s+","").toLowerCase(),"Brooklyn,NY");
                    // User has just created an account, save his/her details to the 'Clients' child
                    clientDatabaseReference.push().setValue(client);
                    // When user is registering for the first time, pushes his/her email ID to the 'Completed Registration' child
                    clientCompletedRegistrationReference.push().setValue(user.getEmail());
                }
                return;
            }
        }

在数据库中,总是(最初)有

--reg_clients
 |
 ---K_3uY0-M4ZCkRSt53SZ: "xyz@gmail.com"  

但日志仍然显示
in onActivityResult user is:XYZ
emailAddressList is:[]
user.getEmail() isfalse

由于此条件为 false,数据将作为新数据推送到 clients 和 reg_clients,因此同一用户的多个条目会在数据库中生成。

然后调用clientCompletedRegistrationValueEventListener 的onDataChange 填充emailAddressList,但它现在没有任何用途,因为已经创建了重复条目。

请在此方法中帮助我,或者如果您有更好的方法,请告诉我。谢谢

编辑:为clientCompletedRegistrationValueEventListener 添加代码

clientCompletedRegistrationValueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot emailSnapshot : dataSnapshot.getChildren()) {
                    Log.d("Adding",emailSnapshot.getValue().toString()+" to emailAddressList");
                    emailAddressList.add(emailSnapshot.getValue().toString());
                    Log.d("in onDataChange","emailAddressList is:"+Arrays.toString(emailAddressList.toArray()));
                }
            }

【问题讨论】:

  • 您将数据存储到 emailAddressList 的位置
  • @SaurabhPadwekar 这是我在该活动中使用的全局变量。我不会在本地的任何地方保存它,因为addListenerForSingleValueEvent 应该给我存储在“registered_clients”孩子中的所有电子邮件地址。
  • 在此处包含监听器代码
  • 我把它链接到一个要点,但我可以在这里添加它

标签: java android authentication firebase firebase-authentication


【解决方案1】:

当你添加一个监听器时,你不会立即得到值。你必须等待响应。所以,把那个代码放在监听器的 onDataChange 里面。像这样

  clientCompletedRegistrationValueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot emailSnapshot : dataSnapshot.getChildren()) {

                    Log.d("Adding", emailSnapshot.getValue().toString() + " to emailAddressList");
                    emailAddressList.add(emailSnapshot.getValue().toString());
                    Log.d("in onDataChange", "emailAddressList is:" + Arrays.toString(emailAddressList.toArray()));
                }
                    Log.d("in onActivityResult","user is:"+user.getDisplayName());
                    Log.d("in onActivityResult","emailAddressList is:"+ Arrays.toString(emailAddressList.toArray()));
                    Log.d("in onActivityResult","user.getEmail() is"+ emailAddressList.contains(user.getEmail()));

                    if(emailAddressList.contains(user.getEmail())) {
                        Toast.makeText(NavDrawerActivity.this, "You're signed in again", Toast.LENGTH_SHORT).show();
                    } else {
                        // Gets photoUrl when Google+ photo is set, otherwise returns null. Appending '?sz=100' at end gets a photo of 100x100 px
                        String photoUrl = user.getPhotoUrl() != null ? user.getPhotoUrl().toString()+"?sz=100": null;
                        Toast.makeText(NavDrawerActivity.this, "You're signed in for the first time", Toast.LENGTH_SHORT).show();

                        Client client = new Client(user.getDisplayName(),0,photoUrl,user.getDisplayName().replaceAll("\\s+","").toLowerCase(),"Brooklyn,NY");
                        // User has just created an account, save his/her details to the 'Clients' child
                        clientDatabaseReference.push().setValue(client);
                        // When user is registering for the first time, pushes his/her email ID to the 'Completed Registration' child
                        clientCompletedRegistrationReference.push().setValue(user.getEmail());
                    }

                }
            };

【讨论】:

  • 好的,感谢您的回答,让我试试这个并回复您。
  • 那么我应该将clientCompletedRegistrationReference.addListenerForSingleValueEvent(clientCompletedRegistrationValueEventListener); 保留在哪里,保持原样在onActivityResult 中?
  • 我在正确的位置添加了 for 循环的右括号。现在它应该可以工作了
猜你喜欢
  • 1970-01-01
  • 2016-09-26
  • 1970-01-01
  • 1970-01-01
  • 2015-11-08
  • 2014-12-25
  • 2023-03-04
  • 2019-12-31
相关资源
最近更新 更多