I am trying to add phone authentication in my app using Firebase, When I click the send OTP button a browser is opening saying verifying that you are not a robot and only after I verify that I am not a robot by selecting the images, it is sending an OTP.
And also the format of OTP has changed.
Previously the OTP format was
435325 is your verification code for MyApplication.
New format of OTP is
789574 if your verification code for myapplication.55b19.firebaseapp.com
I just don't understand why a browser is opening every time I request for an OTP
Here is my code
public class MainActivity extends AppCompatActivity {
EditText phoneEt,otpEt;
Button loginBtn,verifyOTPBtn;
private String verificationCodeBySystem;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseAuth = FirebaseAuth.getInstance();
phoneEt = findViewById(R.id.phoneEt);
otpEt = findViewById(R.id.otpEt);
loginBtn = findViewById(R.id.loginBtn);
verifyOTPBtn = findViewById(R.id.verifyOTPBtn);
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String phoneNumber = phoneEt.getText().toString().trim();
sendOTPCodeToUser(phoneNumber);
}
});
verifyOTPBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String code = otpEt.getText().toString().trim();
if (code.isEmpty() || code.length() < 6){
otpEt.setError("Wrong OTP...");
otpEt.requestFocus();
return;
}
verifyCode(code);
}
});
}
private void sendOTPCodeToUser(String phoneNumber) {
PhoneAuthOptions options =
PhoneAuthOptions.newBuilder(firebaseAuth)
.setPhoneNumber("+91"+phoneNumber) // Phone number to verify
.setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
.setActivity(this) // Activity (for callback binding)
.setCallbacks(mCallbacks) // OnVerificationStateChangedCallbacks
.build();
PhoneAuthProvider.verifyPhoneNumber(options);
}
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
verificationCodeBySystem = s;
}
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
String code = phoneAuthCredential.getSmsCode();
verifyCode(code);
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
}
};
private void verifyCode(String code) {
try{
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationCodeBySystem,code);
signInTheCredentials(credential);
}catch (Exception e){
}
}
private void signInTheCredentials(PhoneAuthCredential credential) {
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(task -> {
if (task.isSuccessful()){
Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "Wrong OTP", Toast.LENGTH_SHORT).show();
}
});
}
}
Gradle file
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "package.myapplication"
minSdkVersion 24
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.firebase:firebase-auth:20.0.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation platform('com.google.firebase:firebase-bom:26.0.0')
implementation 'android.arch.persistence.room:runtime:1.1.1'
implementation "androidx.browser:browser:1.2.0"
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
}