i want the otp sent by Firebase so i can verify that otp entered by user and otp sent by firebase is same or not so i can proceed furthure..through internet i got that by phoneAuthCredential.getSmsCode() i can get otp sent by firebase.but that credential object is in onverificationcomplete() method which is not getting called in my case.
when i run the app i get otp but when i entered that same otp or wrong otp in both cases the next activity get started...
code :
private void sendVerificationCode(String number) {
// this method is used for getting
// OTP on user phone number.
Log.d("piooo", number);
PhoneAuthOptions options =
PhoneAuthOptions.newBuilder(mAuth)
.setPhoneNumber(number) // Phone number to verify
.setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
.setActivity(this) // Activity (for callback binding)
.setCallbacks(mCallBack) // OnVerificationStateChangedCallbacks
.build();
PhoneAuthProvider.verifyPhoneNumber(options);
}
// callback method is called on Phone auth provider.
private PhoneAuthProvider.OnVerificationStateChangedCallbacks
mCallBack = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
// below line is used for getting OTP code
// which is sent in phone auth credentials.
Log.d("piooo","verification completed ");
codesent = phoneAuthCredential.getSmsCode();
Log.d("piooo firebasecodesent", codesent);
}
@Override
public void onVerificationFailed(FirebaseException e) {
// displaying error message with firebase exception.
Log.d("piooo fail", "");
Toast.makeText(Registration.this, e.getMessage(), Toast.LENGTH_SHORT).show();
Toast.makeText(Registration.this, "Try Again", Toast.LENGTH_SHORT).show();
registerbtn.setEnabled(true);
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
// when we receive the OTP it
// contains a unique id which
Log.d("piooo code sent", "");
Log.d("piooo otp code", s);
verificationId = s;
progressDialog.dismiss();
alertDialog.show();
otp1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
otp2.requestFocus();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
otp2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
otp3.requestFocus();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
otp3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
otp4.requestFocus();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
otp4.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
otp5.requestFocus();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
otp5.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
otp6.requestFocus();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
};
below code is for verify both codes ..if i make PhoneAuthCredential object here and call method getsmscode() then i get the code which is user inputed code but i want code sent by firebase.
// below method is use to verify code from Firebase.
private void verifyCode(String code) {
Log.d("piooo verify code fun", "");
Log.d("piooo comparison", "code="+code+"codesent="+codesent);
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
codesent=credential.getSmsCode();
Log.d("piooo codesent", codesent);
if (code.equals(codesent)) {
Toast.makeText(Registration.this, "Ready To Go", Toast.LENGTH_LONG).show();
Loading.setVisibility(View.GONE);
check.setVisibility(View.VISIBLE);
user user = new user("default", user_name.getText().toString(), user_mail.getText().toString(),
user_password.getText().toString(), user_mobile_no.getText().toString());
database.getReference().child("Users").child(authwithmail_uid).setValue(user);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent i = new Intent(Registration.this, custHomeActivity.class);
Log.d("piooo reg uidd maauth", mAuth.getCurrentUser().getUid());
i.putExtra("userid", authwithmail_uid);
startActivity(i);
finish();
}
}, 2000);
}
else{
Loading.setVisibility(View.GONE);
incorrect_otp.setVisibility(View.VISIBLE);
// check.setVisibility(View.VISIBLE);
Toast.makeText(Registration.this,"Incorrect OTP!!...Try again",Toast.LENGTH_LONG).show();
registerbtn.setEnabled(true);
}
}
my build.gradle file
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.barberr"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
// buildFeatures{
// viewBinding true
// }
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation platform('com.google.firebase:firebase-bom:29.1.0')
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.android.gms:play-services-auth:20.1.0'
implementation 'com.google.firebase:firebase-database:20.0.3'
implementation 'com.google.firebase:firebase-storage:20.0.0'
implementation 'com.google.firebase:firebase-core:20.1.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.google.firebase:firebase-auth:21.0.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'de.hdodenhof:circleimageview:3.1.0'
implementation 'androidx.browser:browser:1.4.0'
}