3

因此,我正在尝试使用 Firebase 创建登录和注册流程,主要要求仅使用电话号码。

但我发现我只能使用 createUserWithEmailAndPassword 来注册用户。现在的问题是如何使用电话号码注册用户。

如果不是,那么firebase如何使用电话号码登录用户?

输入电话号码和otp后两种情况

  1. 如果用户已经存在,他会去他的仪表板
  2. 如果他是应用程序的新手,他会转到注册详细信息页面,然后转到仪表板
4

1 回答 1

0

将电话号码登录添加到您的应用的最简单方法是使用 FirebaseUI,它包括一个插入式登录小部件,用于实现电话号码登录以及基于密码和联合登录的登录流程-在。

希望您已经添加了 Firebase 依赖项并启用了身份验证。在此之后,此代码将指导您:

  PhoneAuthOptions options = 
  PhoneAuthOptions.newBuilder(mAuth) 
      .setPhoneNumber(phoneNumber)       // Phone number to verify
      .setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
      .setActivity(this)                 // Activity (for callback binding)
      .setCallbacks(mCallbacks)          // OnVerificationStateChangedCallbacks
      .build();
  PhoneAuthProvider.verifyPhoneNumber(options);   

如果您想在验证成功后重定向用户,那么 Firebase 预装了所需的方法:

@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
    // This callback will be invoked in two situations:
    // 1 - Instant verification. In some cases the phone number can be instantly
    //     verified without needing to send or enter a verification code.
    // 2 - Auto-retrieval. On some devices Google Play services can automatically
    //     detect the incoming verification SMS and perform verification without
    //     user action.
    Log.d(TAG, "onVerificationCompleted:" + credential);

    signInWithPhoneAuthCredential(credential);
}

@Override
public void onVerificationFailed(FirebaseException e) {
    // This callback is invoked in an invalid request for verification is made,
    // for instance if the the phone number format is not valid.
    Log.w(TAG, "onVerificationFailed", e);

    if (e instanceof FirebaseAuthInvalidCredentialsException) {
        // Invalid request
        // ...
    } else if (e instanceof FirebaseTooManyRequestsException) {
        // The SMS quota for the project has been exceeded
        // ...
    }

    // Show a message and update the UI
    // ...
}

@Override
public void onCodeSent(@NonNull String verificationId,
                       @NonNull PhoneAuthProvider.ForceResendingToken token) {
    // The SMS verification code has been sent to the provided phone number, we
    // now need to ask the user to enter the code and then construct a credential
    // by combining the code with a verification ID.
    Log.d(TAG, "onCodeSent:" + verificationId);

    // Save verification ID and resending token so we can use them later
    mVerificationId = verificationId;
    mResendToken = token;

    // ...
}
于 2020-12-12T05:14:13.483 回答