我如何检测是否有人输入了错误的 otp
how can i detect if someone entered wrong otp
我只是想在用户输入错误的 otp 时向他显示错误,我该如何实现,因为如果他输入错误的代码,应用程序就会崩溃。
这是我的代码:
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
auth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
proceedProgressDialog.dismiss();
FirebaseUser user = auth.getCurrentUser();
String currentUserMobile = user.getPhoneNumber();
CollectionReference reference = FirebaseFirestore.getInstance().collection("Users");
Query query = reference.whereEqualTo("Mobile", currentUserMobile);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot snapshot : task.getResult()) {
String userCred = snapshot.getString("Mobile");
if (userCred.equals(currentUserMobile)) {
if (user != null) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString(KEY_MOBILE, currentUserMobile);
editor.commit();
Intent intent = new Intent(LoginActivity.this, DashboardActivity.class);
startActivity(intent);
finishAffinity();
}
}
}
}
if (task.getResult().size() == 0) {
Toast.makeText(LoginActivity.this, "User Not Exist", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, AdditionalDetailsActivity.class);
startActivity(intent);
finish();
}
}
});
} else {
// Here I want to show wrong otp error
}
}
});
}
我想检查是否有人输入错误的 otp 可能会显示错误
if (task.getException() instanceof
FirebaseAuthInvalidCredentialsException) {
Toast.makeText(Verification.this, "Incorrect OTP entered", Toast.LENGTH_LONG).show();}
That will put in the else part where i write a comment to put error
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
proceedProgressDialog.dismiss();
binding.getStartedOTP.setError("Invalid OTP");
Toast.makeText(LoginActivity.this, "Incorrect OTP entered", Toast.LENGTH_LONG).show();
}
我只是想在用户输入错误的 otp 时向他显示错误,我该如何实现,因为如果他输入错误的代码,应用程序就会崩溃。
这是我的代码:
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
auth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
proceedProgressDialog.dismiss();
FirebaseUser user = auth.getCurrentUser();
String currentUserMobile = user.getPhoneNumber();
CollectionReference reference = FirebaseFirestore.getInstance().collection("Users");
Query query = reference.whereEqualTo("Mobile", currentUserMobile);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot snapshot : task.getResult()) {
String userCred = snapshot.getString("Mobile");
if (userCred.equals(currentUserMobile)) {
if (user != null) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString(KEY_MOBILE, currentUserMobile);
editor.commit();
Intent intent = new Intent(LoginActivity.this, DashboardActivity.class);
startActivity(intent);
finishAffinity();
}
}
}
}
if (task.getResult().size() == 0) {
Toast.makeText(LoginActivity.this, "User Not Exist", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, AdditionalDetailsActivity.class);
startActivity(intent);
finish();
}
}
});
} else {
// Here I want to show wrong otp error
}
}
});
}
我想检查是否有人输入错误的 otp 可能会显示错误
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) { Toast.makeText(Verification.this, "Incorrect OTP entered", Toast.LENGTH_LONG).show();}
That will put in the else part where i write a comment to put error
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
proceedProgressDialog.dismiss();
binding.getStartedOTP.setError("Invalid OTP");
Toast.makeText(LoginActivity.this, "Incorrect OTP entered", Toast.LENGTH_LONG).show();
}