Android 如何在Firebase实时数据库中进行每次OTP验证?

Android 如何在Firebase实时数据库中进行每次OTP验证?,android,firebase,firebase-realtime-database,firebase-authentication,one-time-password,Android,Firebase,Firebase Realtime Database,Firebase Authentication,One Time Password,我在android studio中创建了一个应用程序,并将其与Firebase实时数据库链接。我已经使用它的通知服务发送OTP来验证用户手机号 我面临的问题是,注销后,我想再次向用户输入的号码发送OTP,但不是验证号码并自动打开活动。有人能帮我吗?这种方法可以将OTP发送到您的生产任务编号 PhoneAuthProvider.getInstance().verifyPhoneNumber( phoneNumber, // Phone number to verify

我在android studio中创建了一个应用程序,并将其与Firebase实时数据库链接。我已经使用它的通知服务发送OTP来验证用户手机号


我面临的问题是,注销后,我想再次向用户输入的号码发送OTP,但不是验证号码并自动打开活动。有人能帮我吗?

这种方法可以将OTP发送到您的生产任务编号

    PhoneAuthProvider.getInstance().verifyPhoneNumber(
    phoneNumber,        // Phone number to verify
    60,                 // Timeout duration
    TimeUnit.SECONDS,   // Unit of timeout
    this,               // Activity (for callback binding)
    mCallbacks);  
您需要回电话才能得到回复

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

    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(String verificationId,
                       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;

    // ...
}};
用于验证OTP调用下面的方法

 PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);


 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();
                    // ...
                } 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
                    }
                }
            }
        });}
PhoneAuthCredential credential=PhoneAuthProvider.getCredential(验证ID,代码);
使用PhoneAuthCredential(PhoneAuthCredential凭据)的专用无效登录{
mAuth.SIGNWITH凭证(凭证)
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
//登录成功,使用登录用户的信息更新UI
Log.d(标记“signInWithCredential:success”);
FirebaseUser=task.getResult().getUser();
// ...
}否则{
//登录失败,显示消息并更新UI
w(标记“signInWithCredential:failure”,task.getException());
if(FirebaseAuthInvalidCredentialsException的task.getException()实例){
//输入的验证码无效
}
}
}
});}

此方法可以将OTP发送到您的生产任务编号

    PhoneAuthProvider.getInstance().verifyPhoneNumber(
    phoneNumber,        // Phone number to verify
    60,                 // Timeout duration
    TimeUnit.SECONDS,   // Unit of timeout
    this,               // Activity (for callback binding)
    mCallbacks);  
您需要回电话才能得到回复

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

    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(String verificationId,
                       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;

    // ...
}};
用于验证OTP调用下面的方法

 PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);


 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();
                    // ...
                } 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
                    }
                }
            }
        });}
PhoneAuthCredential credential=PhoneAuthProvider.getCredential(验证ID,代码);
使用PhoneAuthCredential(PhoneAuthCredential凭据)的专用无效登录{
mAuth.SIGNWITH凭证(凭证)
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
//登录成功,使用登录用户的信息更新UI
Log.d(标记“signInWithCredential:success”);
FirebaseUser=task.getResult().getUser();
// ...
}否则{
//登录失败,显示消息并更新UI
w(标记“signInWithCredential:failure”,task.getException());
if(FirebaseAuthInvalidCredentialsException的task.getException()实例){
//输入的验证码无效
}
}
}
});}

请在注销时执行此操作设置标志用户注销和再次登录的时间检查标志和工作一致请在注销时执行此操作设置标志用户注销和再次登录的时间检查标志和工作一致我已经创建了一个安卓应用程序,我只想验证一次otp(假设同一个人有两部手机,想用第二部手机玩应用程序,那么号码应该从firebase自动验证,otp不应该发送到同一个号码)请帮我解决这个问题。我认为如果你想再次登录是不可能的,那么你应该使用电话号码或电子邮件密码登录,然后每次firebase发送OTP并再次验证时,我想验证firebase中是否有电话号码,如果没有,则不应授予下一个活动的访问权限,如果有号码,则访问s应该是自动的需要将home按钮添加到我的应用程序栏和呼叫活动我已经创建了一个android应用程序,我只想验证一次otp(假设同一个人有两部手机,并且想用第二部手机玩该应用程序,那么号码应该从firebase自动验证,otp不应该发送到同一个号码)请帮我解决这个问题。我认为如果你想再次登录是不可能的,那么你应该使用电话号码或电子邮件密码登录,然后每次firebase发送OTP并再次验证时,我想验证firebase中是否有电话号码,如果没有,则不应授予下一个活动的访问权限,如果有号码,则访问s应该是自动的,需要将主页按钮添加到我的应用程序栏,并从此处调用activityt