Android Firebase电话号码验证更新配置文件错误

时间:2018-04-08 17:03:42

标签: android firebase firebase-authentication

我只使用Firebase进行电话号码身份验证,使用FirebaseUI库来管理身份验证阶段,如下所示:

<requires-extension name="backoffice"/>

活动结果由startActivityForResult( AuthUI.getInstance() .createSignInIntentBuilder() .setIsSmartLockEnabled(true) .setAvailableProviders( Arrays.asList(new AuthUI.IdpConfig.Builder(AuthUI.PHONE_VERIFICATION_PROVIDER).build())) .build(), RC_SIGN_IN); 以这种方式处理:

onActivityResult

我在注册后尝试做的是更新FirebaseUser实例上的电子邮件和显示名称:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        IdpResponse response = IdpResponse.fromResultIntent(data);

        if (resultCode == RESULT_OK) {
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            afterRegister(user);
            finish();
            return;
        } else {
            // Sign in failed
            if (response == null) {
                // User pressed back button
                ToastUtil.showSnackbar(this,R.string.sign_in_cancelled);
                return;
            }

            if (response.getErrorCode() == ErrorCodes.NO_NETWORK) {
                ToastUtil.showSnackbar(this,R.string.no_internet_connection);
                return;
            }

            if (response.getErrorCode() == ErrorCodes.UNKNOWN_ERROR) {
                ToastUtil.showSnackbar(this,R.string.unknown_error);
                return;
            }
        }

        ToastUtil.showSnackbar(this,R.string.unknown_sign_in_response);
    }
}

更新操作无法抛出该异常:

final String email = "myuser@email.com";
final FirebaseUser firebaseUser =  FirebaseAuth.getInstance().getCurrentUser();
firebaseUser.updateEmail(email).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        boolean success = task.isSuccessful();
        if(!success){
            task.getException().printStackTrace();
        }
    }
});

我现在的问题是:我怎样才能再次登录&#34;?这个具体案例的文档很差。

1 个答案:

答案 0 :(得分:1)

更改电子邮件是敏感信息,如果您捕获异常 FirebaseAuthRecentLoginRequiredException ,则必须使用OTP重新认证用户,然后再次返回以完成更新用户电子邮件的任务。 对我来说,这是工作。

btnSave.setOnClickListener {
        FirebaseAuth.getInstance().currentUser?.updateEmail(edEmail.text.toString())
            ?.addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    Log.d("UPDATE EMAIL", "User email address updated success.")
                }else{
                    try {
                        throw task.exception!!
                    } catch (e: FirebaseAuthRecentLoginRequiredException) {
                        /**
                         * handle this to re-auth User, e.g Open OTP page
                         * */
                    } catch (e: FirebaseAuthInvalidCredentialsException) {
                    } catch (e: FirebaseAuthUserCollisionException) {
                    } catch (e: Exception) {
                    }
                }
            }
    }

您可以在此处阅读如何使用重要说明更新电子邮件: https://firebase.google.com/docs/auth/android/manage-users