这是我用于firebase登录的代码。完成验证后,我正在退出。但是,当我尝试使用相同的号码再次登录时,它不会发送给我otp。
帮我注册一下。 提前致谢。 这是代码:
@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 verificaiton without
// user action.
// Log.d(TAG, "onVerificationCompleted:" + credential);
//mAuth.signOut();
mAuth= FirebaseAuth.getInstance();
if(mAuth!=null)
mAuth.signOut();
Toast.makeText(MainActivity.this,"verification complete",Toast.LENGTH_SHORT).show();
}
@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);
Toast.makeText(MainActivity.this,"code sent",Toast.LENGTH_SHORT).show();
// Save verification ID and resending token so we can use them later
mVerificationId = verificationId;
mResendToken = token;
mobileNumber.setVisibility(View.GONE);
submit.setVisibility(View.GONE);
otpButton.setVisibility(View.VISIBLE);
otpText.setVisibility(View.VISIBLE);
t1.setVisibility(View.GONE);
t2.setVisibility(View.VISIBLE);
mAuth= FirebaseAuth.getInstance();
}
otpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId,otpText.getText().toString());
signInWithPhoneAuthCredential(credential);
}
});
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
"+91"+mobileNumber.getText().toString(),// Phone number to verify
60,// Timeout duration
TimeUnit.SECONDS,// Unit of timeout
MainActivity.this,// Activity (for callback binding)
mCallbacks);
}
});
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(MainActivity.this,"verification
done",Toast.LENGTH_SHORT).show();
FirebaseUser user = task.getResult().getUser();
} else {
if (task.getException() instanceof
FirebaseAuthInvalidCredentialsException) {
Toast.makeText(MainActivity.this,"verification failed code
invalid",Toast.LENGTH_SHORT).show();
}
}
}
});
}
答案 0 :(得分:0)
Firebase SMS授权使用具有立即验证的电话号码的逻辑。
这意味着当您在真实设备上运行应用并要求发送SMS时:
让我们看一下方法
/// [verificationCompleted] This callback must be implemented.
/// It will trigger when an SMS is auto-retrieved or the phone number has
/// been instantly verified. The callback will receive an [AuthCredential]
/// that can be passed to [signInWithCredential] or [linkWithCredential].
/// [codeSent] Optional callback.
/// It will trigger when an SMS has been sent to the users phone,
/// and will include a [verificationId] and [forceResendingToken].
///
/// [codeAutoRetrievalTimeout] Optional callback.
/// It will trigger when SMS auto-retrieval times out and provide a
/// [verificationId].
Future<void> verifyPhoneNumber({
@required String phoneNumber,
@required Duration timeout,
int forceResendingToken,
@required PhoneVerificationCompleted verificationCompleted,
@required PhoneVerificationFailed verificationFailed,
@required PhoneCodeSent codeSent,
@required PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout,
}
您会看到有“ [verificationCompleted]此回调必须实现。”。
因此,只需将此逻辑添加到您的代码中即可。例如:
final PhoneVerificationCompleted verificationCompleted =
(AuthCredential phoneAuthCredential) async {
user = (await _auth.signInWithCredential(phoneAuthCredential)).user;
print('Phone number already checked.');
};
答案 1 :(得分:0)