【问题标题】:FireBase Phone AuthentifiationFireBase 电话身份验证
【发布时间】:2021-07-13 10:18:28
【问题描述】:

所以基本上我实现了手机认证方法,它工作正常,我每次都在手机上收到 OTP。但问题是我无法从 OnCodeSent 方法中提取代码

 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;
}

当我尝试显示 VerficationId 时,它会显示一个随机字符串,例如 AhfjnVDscqQHBFEFvCHdBVQHVQJNCvcHFBhbHBC,而不是我收到的 OTP (523410)。我该如何解决这个问题?

【问题讨论】:

  • 是的,它是如何工作的。您无法显示应用程序中的代码。发送代码后,您将获得验证 ID,然后使用验证 ID 和估算的代码对用户进行身份验证。如果有相同的,它将验证用户,但如果没有,则不会
  • 问题是当我试图验证代码时,我必须测试 VerificationId = OTP_Received 。这不起作用,因为应用程序将 OTP (523410) 与该随机字符串 (AhfjnVDscqQHBFEFvCHdBVQHVQJNCvcHFBhbHBC) 进行比较
  • 对不起,您没有自己做验证。 PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);获取code时使用verificationId和用户获取的code,使用上述方法获取他们的凭证。
  • firebase.google.com/docs/auth/android/phone-auth#java_4 在我尝试向您发送完整的答案分解时点击此链接
  • 如果我使用 PhoneAuthCredential 方法,如何让我的应用在不登录的情况下验证代码匹配,我想知道具体怎么做。我想验证 OTP_Receied 和 VerficationId 中的代码是否匹配,如果为真则显示 Toast。有人能把代码发给我吗

标签: java android firebase firebase-authentication one-time-password


【解决方案1】:

从这里开始,回调 in 在代码的其他地方声明。

创建一个名为验证 ID 的公共字符串

    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);

    


mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

    @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);
    }

    @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
        mResendToken = token;
        signInCurrentUser(verificationId)
     }
    };
public void signInCurrentUser(String verificationId) {
    
        PhoneAuthCredential authCredential = PhoneAuthProvider.getCredential(verificationId, getCode());
        signInWithPhoneAuthCredential(authCredential);
    
}

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
        .addOnCompleteListener(this, 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 = task.getResult().getUser();
                    // Update UI
                } else {
                    // Sign in failed, display a message and update the UI
                    Log.w(TAG, "signInWithCredential:failure", task.getException());
                    if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                        // The verification code entered was invalid
                    }
                }
            }
        });
   }

get code 方法获取用户输入

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-10
    • 2021-06-10
    • 2018-12-14
    • 2018-12-06
    • 2019-04-14
    • 1970-01-01
    相关资源
    最近更新 更多