I want user to move only further if the OTP is correct and then create an account with Email and then link it with phone credentials. And for this I am trying the following.. I have tried making changes here and there but no success.. If the OTP is wrong it still moves further and creates an account with email and password. What I am doing wrong here pls correct me. Thanks.
Future<void>verifyPhone(String code){
try{
AuthCredential phoneAuthCredential = PhoneAuthProvider.credential(verificationId: vid, smsCode: code);
auth.signInWithCredential(phoneAuthCredential).then((valueSignIn){
auth.createUserWithEmailAndPassword(email: _email, password: _password).then((valueEmail) {
auth.currentUser.linkWithCredential(phoneAuthCredential).then((valueLink) {
insertData();
Navigator.push(context, MaterialPageRoute(builder: (context)=>HomeScreen()));
}).catchError((err){print("phone auth error 1: $err");
print("phone auth error code 1: ${err.code}");
if(err.code == "invalid-verification-code"){
Fluttertoast.showToast( msg: "Invalid OTP, Try Again Or Check Your Phone Number",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 3,
backgroundColor: Colors.blueGrey,
textColor: Colors.white,
fontSize: 16.0);
}else{
Fluttertoast.showToast( msg: "Something Went Wrong, Try Again",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 3,
backgroundColor: Colors.blueGrey,
textColor: Colors.white,
fontSize: 16.0);
}
}).catchError((err){
print("phone auth error 2: $err");
print("phone auth error code 2: ${err.code}");
if(err.code == "invalid-verification-code"){
Fluttertoast.showToast( msg: "Invalid OTP, Try Again Or Check Your Phone Number",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 3,
backgroundColor: Colors.blueGrey,
textColor: Colors.white,
fontSize: 16.0);
}else{
Fluttertoast.showToast( msg: "Something Went Wrong, Try Again",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 3,
backgroundColor: Colors.blueGrey,
textColor: Colors.white,
fontSize: 16.0);
}
});
});
}).catchError((err){
print("phone auth error 3: $err");
print("phone auth error code 3: ${err.code}");
if(err.code == "invalid-verification-code"){
Fluttertoast.showToast( msg: "Invalid OTP, Try Again Or Check Your Phone Number",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 3,
backgroundColor: Colors.blueGrey,
textColor: Colors.white,
fontSize: 16.0);
}else{
Fluttertoast.showToast( msg: "Something Went Wrong, Try Again",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 3,
backgroundColor: Colors.blueGrey,
textColor: Colors.white,
fontSize: 16.0);
}
});
}catch(e){
print("Phone Auth Credential $e");
}
}