WE are migrating an android app to flutter. We have implemented firebase based otp verification. In android, everything works perfectly. The user login and OTP verification works smoothly and the auto verification works fine
However, in flutter, the OTP auto verification does not work. The onVerificationCompleted does not get called when the user received an SMS.
Can someone give a clue on how to enforce the auto verification when an SMS is received?
auth.verifyPhoneNumber(
phoneNumber: user_phone,
timeout: Duration(seconds: 60),
verificationCompleted: (PhoneAuthCredential credential) {
var sms_code = credential.smsCode.toString();
},
verificationFailed: (FirebaseAuthException e) {
var verification_failed_msg = e.message;
},
codeSent: (String verificationId, int? resendToken) {
verificationID = verificationId;
// _controller.resume;
// setState(() {});
},
codeAutoRetrievalTimeout: (String verificationId) {
print("SmsReadTimeout");
},
);
CodePudding user response:
In Android side for Auto-SMS reading you need to include in your SMS the hash of your app at the bottom of the sent text, as described here in 4th point of the list.
Your SMS should be something like:
Hello, this is your OTP: 123456
#{YOUR_APP_SIGNATURE_HASH}
Google gives you this AppSignatureHelper.java class to calculate the hash at runtime for each different keystore used to sign your app. Usually you calculate the hash once running the APK signed with test/debug keystore and once with prod keystore (at least).
PS: you can remove the class from the android sources once you have the hashes, they won't change if you don't change signing keystores.
Once you add the hash to your SMSs, you should start receives them in the verificationCompleted
callback.
CodePudding user response:
I have found the problem and solution for it. Basically, the google recaptcha was preventing the auto verification of the SMS code. As soon as we removed the google auto recaptcha, the app started verifying the code.
There is a separate topic on how to remove google recaptcha from firebase otp verification