我正在尝试通过在我的Android应用中使用Firebase来验证我的OTP。当我触发验证方法时,它显示为
在没有任何verifyProof的情况下无法创建PhoneAuthCredential, sessionInfo,临时证明
由于我不知道我做错了什么,请帮助我解决这个问题。
这是我的第一项活动
public class LoginActivity extends AppCompatActivity {
EditText phonetextview;
String phone_number;
FirebaseAuth auth;
String codesent;
String otp;
FirebaseAuth mAuth;
private PhoneAuthProvider.ForceResendingToken mResendToken;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();
Intent intent = getIntent();
otp = intent.getStringExtra("otp");
Toast.makeText(getBaseContext(),otp,Toast.LENGTH_SHORT).show();
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
verify();
}
});
CardView card_view = findViewById(R.id.cardView);
card_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
phonetextview =findViewById(R.id.phonetextview);
phone_number = PhoneNumberUtils.formatNumber(phonetextview.getText().toString());
send();
Intent i = new Intent(getApplicationContext(),Otp.class);
startActivity(i);
Toast toast = Toast.makeText(getApplicationContext(), phone_number,
Toast.LENGTH_SHORT);
toast.show();
}
});
}
private void send() {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
"+91"+phone_number, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks); // ForceResendingToken from callbacks
}
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks =new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
}
@Override
public void onVerificationFailed(FirebaseException e) {
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
codesent =s;
Toast.makeText(getBaseContext(),codesent,Toast.LENGTH_LONG).show();
}
};
private void verify()
{
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codesent, otp);
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");
FirebaseUser user = task.getResult().getUser();
System.exit(0);
} else {
Toast.makeText(getBaseContext(),"ffffffffffff",Toast.LENGTH_SHORT).show();
// 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
}
}
}
});
}
}
这是我第二次键入otp
public class Otp extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_otp);
String codesent ;
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText fd = (EditText) findViewById(R.id.et1);
String value= fd.getText().toString();
// int finalValue=Integer.parseInt(value);
final EditText sd = (EditText) findViewById(R.id.et2);
String value1= sd.getText().toString();
// int finalValue1=Integer.parseInt(value1);
EditText td = (EditText) findViewById(R.id.et3);
String value2= td.getText().toString();
// int finalValue2=Integer.parseInt(value2);
EditText fod = (EditText) findViewById(R.id.et4);
String value3= fod.getText().toString();
//int finalValue3=Integer.parseInt(value3);
EditText fid = (EditText) findViewById(R.id.et5);
String value4= fid.getText().toString();
//int finalValue4=Integer.parseInt(value4);
EditText sid = (EditText) findViewById(R.id.et6);
String value5= sid.getText().toString();
//int finalValue5=Integer.parseInt(value5);
final String otp = value + value1+value2+value3+value4+value5;
Intent intent = new Intent(v.getContext(), LoginActivity.class);
intent.putExtra("otp", otp);
startActivity(intent);
finish();
}
});
}
}