gpt4 book ai didi

android - FirebaseAuthUserCollisionException?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:03:21 25 4
gpt4 key购买 nike

我正在使用 firebase 文档来使用 Facebook 登录我的 android 应用程序。我成功地能够使用 Google 和 Twitter 登录用户。但是当我点击使用 Facebook 登录时,该按钮变为注销按钮。实际上它应该重定向到登录成功 Activity ,因为我配置了 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。我应该在 Stackoverflow 中发布问题之前调试应用程序。

更新2

我不会关闭这个问题,因为它可能会帮助遇到像我这样的情况的人。我还将更新 FirebaseAuthUserCollisionException 的解决方案,因为我会深入研究它。

最佳答案

我遇到了同样的问题,这就是我解决问题的方式。
如果任务不成功,我将检查抛出的异常是否是 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

关于android - FirebaseAuthUserCollisionException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37763961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com