1 year ago
#377284
Mahmoud Eidarous
How can I make the Firebase OTP return the Sms CodeSent so that I use it for verification other than Sign in?
The following firebase function sends the OTP code to the phone number.
Problem is.. _onVerificationCompleted
gets only invoked when the phone number is on the device that is sending the request (SIM card).
What if I need to know the sent otp code when the _onVerificationCompleted
method will never get called as it's a different device phone number?
I tried PhoneAuthProvider.credential(verificationId: verification, smsCode: enteredCode);
But this method's return type is PhoneAuthCredential
, which is only used when trying to signin/login the user.
I just need to verify the entered code == the smsCode without signing in. Any help would be really appreciated!
Future<void> verifyPhoneNumber({@required String phoneNumber}) async {
await _auth.verifyPhoneNumber(
phoneNumber: phoneNumber,
verificationCompleted: _onVerificationCompleted,
verificationFailed: _onVerificationFailed,
codeSent: _onCodeSent,
codeAutoRetrievalTimeout: _onCodeTimeout);
}
_onVerificationCompleted(PhoneAuthCredential authCredential) async {
print("verification completed ${authCredential.smsCode}");
if (otpTextFieldController.text == authCredential.smsCode) {
print('Verified!');
} else {
print('not-verified!');
}
setState(() {
_numberValidated = true;
});
if (authCredential.smsCode != null) {
try {} on FirebaseAuthException catch (e) {
print('OTP error : $e');
}
setState(() {
isLoading = false;
});
}
}
_onVerificationFailed(FirebaseAuthException exception) {
print('verification : ${exception}');
if (exception.code == 'invalid-phone-number') {
showMessage("The phone number entered is invalid!");
}
}
_onCodeSent(String verificationId, int forceResendingToken) {
this.verificationId = verificationId;
print(forceResendingToken);
print("code sent = ${this.verificationId}");
}
_onCodeTimeout(String timeout) {
return null;
}
void showMessage(String errorMessage) {
showDialog(
context: context,
builder: (BuildContext builderContext) {
return AlertDialog(
title: Text("Error"),
content: Text(errorMessage),
actions: [
TextButton(
child: Text("Ok"),
onPressed: () async {
Navigator.of(builderContext).pop();
},
)
],
);
}).then((value) {
setState(() {
isLoading = false;
});
});
}
firebase
flutter
dart
firebase-authentication
one-time-password
0 Answers
Your Answer