[英]Cannot Verify phone number using firebase authentication
我正在嘗試驗證電話號碼,但始終失敗。 我有登錄活動,它獲取有關用戶的所有信息,包括手機號碼字段。 單擊登錄按鈕后,我希望 OTP 活動將打開,用戶可以在此處輸入 otp 並單擊驗證按鈕以驗證號碼。 但它總是進入 onVerificationFailed 塊並顯示 toast Verification Fail 和 Invalid Number。
登錄活動
public class Login extends AppCompatActivity{
public FirebaseAuth mAuth;
// [END declare_auth]
public String mVerificationId;
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
public PhoneAuthProvider.ForceResendingToken mResendToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String name = txtName.getText().toString();
final String mobileno = txtMobile.getText().toString();
final String email = txtEmail.getText().toString();
final String dob = txtDOB.getText().toString();
final String address = txtAdd.getText().toString();
final String city = txtCity.getText().toString();
final String state = txtState.getText().toString();
final String country = txtCountry.getText().toString();
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
Toast.makeText(Login.this,"Verification Done"+ phoneAuthCredential,Toast.LENGTH_LONG).show();
}
@Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(Login.this,"Verification Fail",Toast.LENGTH_LONG).show();
if (e instanceof FirebaseAuthInvalidCredentialsException){
Toast.makeText(Login.this, "Invalid Number", Toast.LENGTH_SHORT).show();
}else if (e instanceof FirebaseTooManyRequestsException){
Toast.makeText(Login.this, "Too many Request", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
mVerificationId = s;
mResendToken = forceResendingToken;
Toast.makeText(Login.this, "Code Sent", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Login.this, OTP.class));
}
};
PhoneAuthProvider.getInstance().verifyPhoneNumber(mobileno, 60, TimeUnit.SECONDS, Login.this, 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()) {
// Sign in success, update UI with the signed-in user's informatio
Toast.makeText(Login.this,"Verification done",Toast.LENGTH_LONG).show();
FirebaseUser user = task.getResult().getUser();
// ...
} else {
// Sign in failed, display a message and update the UI
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
Toast.makeText(Login.this,"Verification failed code invalid",Toast.LENGTH_LONG).show();
}
}
}
});
}
}
OTP活動
public class OTP extends AppCompatActivity {
public FirebaseAuth mAuth;
public String mVerificationId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_otp);
Button btnVerify = (Button) findViewById(R.id.btnVerifyOTP);
final EditText txtOtp = (EditText) findViewById(R.id.txtOtp);
btnVerify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential( mVerificationId , txtOtp.getText().toString());
signInWithPhoneAuthCredential(credential);
}
});
}
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");
Toast.makeText(OTP.this,"Verification done",Toast.LENGTH_LONG).show();
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
Toast.makeText(OTP.this,"Verification failed code invalid",Toast.LENGTH_LONG).show();
}
}
}
});
}
}
Firebase 會自動驗證某些設備上的 SMS 消息。 在您的情況下,它似乎正在嘗試驗證 SMS 消息,但無法驗證,這就是無效電話號碼錯誤的原因,我猜。 但是你在哪個設備上測試?
更重要的是,您的代碼似乎沒有用這個 ... startActivity(new Intent(Login.this, OTP.class)); 來涵蓋自動短信驗證方案。
您可以在此處查看Firebase 電話號碼身份驗證的完整實現http://www.zoftino.com/firebase-phone-number-authentication-android
我遇到了同樣的問題,我所做的是在輸入電話號碼時說如果號碼是 79.....6 ,然后將輸入設為 +9179...6(如果是印度號碼)
根據國家/地區,添加國家/地區代碼作為前綴以獲取短信代碼
希望它有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.