问题描述
我试图使用的AuthCredential
将AuthCredential对象传递给登录用户的linkWithCredential方法,如下所示:
- get AuthCredential for the new authentication provider
Pass the AuthCredential object to the signed-in user's linkWithCredential method, like this:
mAuth.getCurrentUser().linkWithCredential(credential)
由于用户已经通过一个身份验证提供程序进行了签名,因此我们无需再次对其进行签名.我们只需要链接两个提供者,以便他能够再次与任何一个提供者登录.
As user is already signed with one auth provider, we don't need to sign him again. We just need to link both the providers, so that he'll be able to sign in again with either of the providers.
正如代码中的流程所示,在验证了电话号码之后,您将再次使用PhoneAuthCredential登录用户,然后尝试链接emailCredential;当前已登录用户已链接到该用户,因此会出现错误.
As the flow from your code suggests, after verifying the phone number, you are signing the user in again with PhoneAuthCredential, and then you are trying to link the emailCredential; which the current signed in user is already linked to, hence the error.
这应该是您的mCallbacks代码:
This should be your code for mCallbacks:
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { @Override public void onVerificationCompleted(PhoneAuthCredential credential) { Log.d(TAG, "onVerificationCompleted:" + credential); linkCredential(credential); } @Override public void onVerificationFailed(FirebaseException e) { Log.w(TAG, "onVerificationFailed", e); if (e instanceof FirebaseAuthInvalidCredentialsException) { mPhoneNumberField.setError("Invalid phone number."); } else if (e instanceof FirebaseTooManyRequestsException) { Snackbar.make(findViewById(android.R.id.content), "Quota exceeded.", Snackbar.LENGTH_SHORT).show(); } } @Override public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken token) { Log.d(TAG, "onCodeSent:" + verificationId); mVerificationId = verificationId; mResendToken = token; } };
这是linkCredentials方法.
public void linkCredential(AuthCredential credential) { mAuth.getCurrentUser().linkWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { Log.d(TAG, "linkWithCredential:success"); FirebaseUser user = task.getResult().getUser(); Toast.makeText(PhoneActivity.this, "Merged", Toast.LENGTH_SHORT).show(); moveToHome(); } else { Log.w(TAG, "linkWithCredential:failure", task.getException()); Toast.makeText(PhoneActivity.this, "Failed to merge" + task.getException().toString(), Toast.LENGTH_SHORT).show(); } } }); }
这篇关于如何在Firebase中将emailAndPasswordAuth与PhoneAuth合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!