如何将电话身份验证与电子邮件身份验证Firebase Android链接

时间:2020-01-26 08:21:05

标签: android firebase firebase-authentication

我正在创建一个应用程序,用户必须验证其电话号码,并必须使用其电子邮件地址和密码以及其他诸如dob之类的文件进行注册。第一个用户将验证电话号码,因此我已使用电话未进行身份验证,一旦完成,用户将导航至电子邮件和密码注册。因此,此处用户将使用电子邮件身份验证进行注册。其创建两个用户ID。所以我想在一个用户名下进行两种身份验证。

PhoneAuthentication.java

private void signInWithCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {


                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {

                        Toast.makeText(GetOTPActivity.this, "Mobile no authentication is successfully !!!", Toast.LENGTH_SHORT).show();
                        Intent intent;
                        intent = new Intent(GetOTPActivity.this, Register.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

                        startActivity(intent);

                    } else {
                        Toast.makeText(GetOTPActivity.this, task.getException().getMessage(),
                                Toast.LENGTH_LONG).show();
                    }
                }

            });
}

电子邮件身份验证。java

    mAuth.createUserWithEmailAndPassword(email, password)
                    .addOnCompleteListener(Register.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            Toast.makeText(Register.this, "Signup Successfully", Toast.LENGTH_SHORT).show();


                            if (!task.isSuccessful()) {
                                spotsDialog.dismiss();
                                Toast.makeText(Register.this, "Signup failed." + task.getException(),
                                        Toast.LENGTH_SHORT).show();


                            } else {

                                spotsDialog.dismiss();

                                userID = mAuth.getCurrentUser().getUid();
                                User user = new User(edtName.getText().toString(), edtPhone.getText().toString(),
                                        edtEmail.getText().toString(), edtPassword.getText().toString(), edtConfirmPassword.getText().toString(), false);
                                user_tbl.child(userID).setValue(user);
                                startActivity(new Intent(Register.this, Login.class));

                            }
                        }
                    });

        }
    });

1 个答案:

答案 0 :(得分:0)

所有您需要使用的是linkWithCredential(),您必须在其中传递凭据,现在该凭据可以来自任何身份验证,例如facebook,gmail。

尝试此代码:

 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();
                updateUI(user);
            } else {
                Log.w(TAG, "linkWithCredential:failure", task.getException());
                Toast.makeText(AnonymousAuthActivity.this, "Authentication failed.",
                        Toast.LENGTH_SHORT).show();
                updateUI(null);
            }

            // ...
        }
    });