在Firebase API for Android

时间:2016-12-17 05:26:55

标签: android firebase firebase-authentication

我正在尝试自动登录用户,如果他已经注册了。问题是,当我执行getCurrentUser时,它返回多个用户,即使我的用户仅在Facebook注册。我无法弄清楚它为什么显示第一个用户。这是我在启动活动中的authStateListner代码:

mAuth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            int i = 0;
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if(user == null){
                Intent loginIntent = new Intent(StartActivity.this, LoginActivity.class);
                startActivity(loginIntent);
                return;
            }
                for(UserInfo info : firebaseAuth.getCurrentUser().getProviderData()){
                    Log.d(START_ACTIVITY_TAG, "" + i++);
                    if(info == null){
                        Intent loginIntent = new Intent(StartActivity.this, LoginActivity.class);
                        startActivity(loginIntent);
                    }


                    if(info.getProviderId().contentEquals("google.com")){
                        Log.d(START_ACTIVITY_TAG,info.getProviderId());
                        Log.d(START_ACTIVITY_TAG,"Official Login" + info.getEmail());
                        Intent officialIntent = new Intent(StartActivity.this, OfficialActivity.class);
                        officialIntent.putExtra("emailId",info.getEmail());
                        startActivity(officialIntent);
                    }else if(info.getProviderId().contentEquals("facebook.com")){
                        Log.d(START_ACTIVITY_TAG,info.getProviderId());
                        Log.d(START_ACTIVITY_TAG,"Resident Login through fb");
                        Intent reportIntent = new Intent(StartActivity.this, ReportActivity.class);
                        reportIntent.putExtra("resident_id", info.getEmail());
                        startActivity(reportIntent);
                    }else{
                        Log.d(START_ACTIVITY_TAG,info.getProviderId());
                        Log.d(START_ACTIVITY_TAG,"Resident Login normal" + info.getEmail());
                        Intent reportIntent = new Intent(StartActivity.this, ReportActivity.class);
                        reportIntent.putExtra("resident_id",info.getEmail());
                        startActivity(reportIntent);
                    }
            }
        }
    });

此代码块的我的日志如下:

D/Start_Activity: firebase
D/Start_Activity: Resident Login normalnull
D/Start_Activity: 1
D/Start_Activity: facebook.com
D/Start_Activity: Resident Login through fb
D/Start_Activity: 0
D/Start_Activity: firebase
D/Start_Activity: Resident Login normalnull
D/Start_Activity: 1
D/Start_Activity: facebook.com
D/Start_Activity: Resident Login through fb

1 个答案:

答案 0 :(得分:0)

输出看起来像我预期的那样。

第一个输出显示Firebase内部创建的UserInfo。然后第二个输出显示从Facebook获得的提供者数据。