FirebaseAuthUserCollisionException?

Posted

技术标签:

【中文标题】FirebaseAuthUserCollisionException?【英文标题】:FirebaseAuthUserCollisionException? 【发布时间】:2016-10-12 08:23:29 【问题描述】:

我正在使用 firebase 文档来使用 Facebook 登录我的 android 应用程序。我能够成功地使用 Google 和 Twitter 登录用户。但是,当我单击“使用 Facebook 登录”时,该按钮将变为“注销”按钮。实际上它应该重定向到登录成功活动,因为我配置了FirebaseAuth.AuthStateListener

我在这里发布Activity,如果我做错了什么,请告诉我。

public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener 

    private static final String TAG = "MainActivity";
    private static final int GOOGLE_RC_SIGN_IN = 9001;
    private static final int TWITTER_RC_SIGN_IN = 140;

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthStateListener;

    private GoogleApiClient mGoogleApiClient;
    CallbackManager callbackManager;
    @BindView(R.id.google_signin_button) SignInButton mGoogleSigninButton;
    @BindView(R.id.facebook_login_button) LoginButton mFacebookLoginButton;
    @BindView(R.id.twitter_login_button) TwitterLoginButton mTwitterLoginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        TwitterAuthConfig authConfig = new TwitterAuthConfig(getString(R.string.twitter_consumer_key), getString(R.string.twitter_consumer_secret));
        Fabric.with(this, new Twitter(authConfig));
        FacebookSdk.sdkInitialize(this);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(MainActivity.this);

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this,this)
                .addApi(Auth.GOOGLE_SIGN_IN_API,gso)
                .build();

        mAuth = FirebaseAuth.getInstance();
        mAuthStateListener = new FirebaseAuth.AuthStateListener()
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) 
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) 
                    //User is signed in
                    Log.v(TAG, "Yo Baby");
                    Log.d(TAG,"name"+user.getDisplayName());
                    goToHome();
                 else 
                    //Do some logic
                
            
        ;

        mGoogleSigninButton.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                Intent signinIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
                startActivityForResult(signinIntent, GOOGLE_RC_SIGN_IN);
            
        );

        callbackManager = CallbackManager.Factory.create();
        mFacebookLoginButton.setReadPermissions("email", "public_profile");
        mFacebookLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() 
            @Override
            public void onSuccess(LoginResult loginResult) 
                handleFacebookAuthentication(loginResult.getAccessToken());
            

            @Override
            public void onCancel() 

            

            @Override
            public void onError(FacebookException error) 

            
        );

        mTwitterLoginButton.setCallback(new Callback<TwitterSession>() 
            @Override
            public void success(Result<TwitterSession> result) 
                handleTwitterAuthentication(result.data);
            

            @Override
            public void failure(TwitterException exception) 

            
        );

    

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

        if (requestCode == GOOGLE_RC_SIGN_IN)
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if (result.isSuccess()) 
                GoogleSignInAccount account = result.getSignInAccount();
                handleGoogleAuthentication(account);
             else 
                //Google Login Failed
            
         else if (requestCode == TWITTER_RC_SIGN_IN) 
            mTwitterLoginButton.onActivityResult(requestCode,resultCode,data);
         else 
            callbackManager.onActivityResult(requestCode,resultCode,data);
        
    

    private void handleGoogleAuthentication(GoogleSignInAccount account) 
        AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
        mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() 
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) 
                if (task.isSuccessful()) 
                    //Save Credentials in Google Smart Lock
                 else 
                    //
                
            
        );
    

    private void handleFacebookAuthentication(AccessToken accessToken) 
        AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getToken());
        mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() 
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) 
                if (task.isSuccessful()) 
                    //Save Credentials in Google Smart Lock
                 else 

                
            
        );
    

    private void handleTwitterAuthentication(TwitterSession session) 
        AuthCredential credential = TwitterAuthProvider.getCredential(session.getAuthToken().token, session.getAuthToken().secret);
        mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() 
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) 
                if (task.isSuccessful()) 
                    //Save credentials in Google Smart lock
                 else 

                
            
        );
    
    private void goToHome() 
        startActivity(new Intent(MainActivity.this, HomeActivity.class));
        finish();
        return;
    

    @Override
    protected void onStart() 
        super.onStart();
        mAuth.addAuthStateListener(mAuthStateListener);
    

    @Override
    protected void onStop() 
        super.onStop();
        if (mAuthStateListener != null) 
            mAuth.removeAuthStateListener(mAuthStateListener);
        
    

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) 

    

更新1

我终于明白了 Facebook 登录不起作用的原因。这是因为我已经使用 Google Signin 登录了该应用程序。由于我给 Google 和 Facebook 的电子邮件是相同的,所以它给了FirebaseAuthUserCollisionException。在我在 *** 中发布问题之前,我应该调试应用程序。

更新2

我不会结束这个问题,因为它可能会帮助像我这样面临这种情况的人。我还会更新FirebaseAuthUserCollisionException 的解决方案,因为我会更深入地研究它。

【问题讨论】:

你是如何处理碰撞错误的?您能否将这两个提供商合并到一个 Firebase 帐户中? 让我们知道您是如何解决的... 【参考方案1】:

我遇到了同样的问题,这就是我的解决方法。 如果任务不成功,我正在检查抛出的异常是否为instanceof FirebaseAuthUserCollisionException。如果是,则会向用户显示“带有电子邮件的帐户已经存在!”的祝酒词。

private void handleFacebookAccessToken(final AccessToken token) 
    Log.d(TAG, "handleFacebookAccessToken:" + token);

    AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() 
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) 

                    if (!task.isSuccessful()) 
                        Log.w(TAG, "signInWithCredential", task.getException());
                        Toast.makeText(getApplicationContext(), "Firebase Facebook login failed",
                                Toast.LENGTH_SHORT).show();

                        if(task.getException() instanceof FirebaseAuthUserCollisionException) 
                            Toast.makeText(getApplicationContext(), "User with Email id already exists",
                                    Toast.LENGTH_SHORT).show();
                        
                        LoginManager.getInstance().logOut();
                    
                
            );
  

信用:Check if given email exists

【讨论】:

【参考方案2】:

关于 FirebaseAuthUserCollisionException: https://developers.google.com/android/reference/com/google/firebase/auth/FirebaseAuthUserCollisionException

防止用户使用相同的电子邮件地址和不同的身份验证提供商创建多个帐户。 见https://support.google.com/firebase/answer/6400716

【讨论】:

以上是关于FirebaseAuthUserCollisionException?的主要内容,如果未能解决你的问题,请参考以下文章