gpt4 book ai didi

java - firebase.auth.FirebaseUser.linkWithCredential 抛出空指针异常

转载 作者:行者123 更新时间:2023-12-01 18:47:15 35 4
gpt4 key购买 nike

我应该如何将现有用户帐户与不同提供商但同一电子邮件地址关联起来?例如 Google 和 Facebook。

public void handleFacebookAccessToken(final AccessToken token, final Activity activity) {
Log.d(TAG, "handleFacebookAccessToken: " + token);

final AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {

@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
authListener.updateSignedUI(user);
}
}
}).addOnFailureListener(activity, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "signInWithCredential:failure", e);
if (e instanceof FirebaseAuthUserCollisionException) {
handleLinkingFacebookUsers(credential, activity);
}
}
});
}

在下面的方法中,发生了“mAuth.getCurrentUser().linkWithCredential() NullPointerException”。

    private void handleLinkingFacebookUsers(AuthCredential savedFBCredential, Activity activity) {
//AuthCredential credentialFacebook = FacebookAuthProvider.getCredential(token.getToken());
//AuthCredential credentialGoogle = GoogleAuthProvider.getCredential(googleIdToken, null);
mAuth.getCurrentUser()
.linkWithCredential(savedFBCredential) // THIS LINE TROWS NULL POINTER EXCEPTION
.addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "linkWithCredential:success");
FirebaseUser user = task.getResult().getUser();
authListener.updateSignedUI(user);
} else {
Log.w(TAG, "linkWithCredential:failure", task.getException());
authListener.updateSignedUI(null);
}

// ...
}
});

}

Logcat:

com.google.firebase.auth.FirebaseAuthUserCollisionException: An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address.
at com.google.firebase.auth.api.internal.zzdv.zza(com.google.firebase:firebase-auth@@19.2.0:42)
at com.google.firebase.auth.api.internal.zzfc.zza(com.google.firebase:firebase-auth@@19.2.0:19)
at com.google.firebase.auth.api.internal.zzes.zza(com.google.firebase:firebase-auth@@19.2.0:34)
at com.google.firebase.auth.api.internal.zzeu.zza(com.google.firebase:firebase-auth@@19.2.0:98)
at com.google.firebase.auth.api.internal.zzeu.zza(com.google.firebase:firebase-auth@@19.2.0:85)

如有任何建议,我们将不胜感激。谢谢。

最佳答案

mAuth.getCurrentUser() 返回 null FirebaseUser,因为调用它时没有用户登录。您的代码应在调用 FirebaseUser 对象上的方法之前检查这一点,如 getCurrentUser() 的 API 文档所示。 .

关于java - firebase.auth.FirebaseUser.linkWithCredential 抛出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59806356/

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