Firebase - 如何确保已经过身份验证的用户的身份验证不会再次发生?

时间:2016-12-28 13:35:12

标签: java android authentication firebase firebase-authentication

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

@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 ,数据会作为新数据推送到clientsreg_clients,因此同一用户的多个条目正在进行中数据库。

然后调用onDataChange的{​​{1}}来填充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()));
                }
            }

1 个答案:

答案 0 :(得分: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());
                    }

                }
            };