I can't catch the exception of Firebase Auth on flutter - flutter

I'm trying to making a signin feature on my app using firebase auth on flutter.
There are many exceptions I have to deal with but I can't catch them at all.
Here is my code.
Future<void> onSubmitted(
String email, AnimationController controller, BuildContext context) async {
controller.repeat();
FirebaseAuth _auth = FirebaseAuth.instance;
try {
_auth.signInWithEmailAndPassword(email: email, password: "1");
} on PlatformException catch (e) {
debugPrint(e.toString());
} on FirebaseAuthException catch (e) {
debugPrint(e.toString());
}
}
This is simplified form for testing. Anyway it doesn't even reach the catch statement. It raises PlatformException before it reaches the catch statement. I don't understand.
The error code is like this.
Unhandled Exception: [firebase_auth/user-not-found] There is no user record corresponding to this identifier. The user may have been deleted.
I explicitly specified the type of the exception and it's supposed to print the error message but it doesn't even reach debugPrint() function.
How should I catch the exception?

The problem was 'they were actually caught'.
There is no problem in catch statement or anywhere but the behavior of the debugger was the problem. On Android Studio I can adjust the setting so it doesn't stop when the exception raises but on VSCode, no matter what I do, it will stop when the exception is raised before the catch statement.
So if I want to continue to the next lines, I just have to click continue in the debugging toolbar.
But I really hope I can change the setting of the breaks on exceptions on VSCode as well.

According to the official FlutterFire Documentation, you should store the `UserCredential`
retrieved from method `signInWithEmailAndPassword()` object in a variable. So your code should look something like this:
try {
UserCredential userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(
email: email,
password: "1"
);
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
print('No user exists with this email.');
} else if (e.code == 'wrong-password') {
print('Incorrect Password.');
}
}

Have you done all the necessary?
Create the ios/Android apps in Firebase console
Enable the different sign-in methods (google, email, etc.)
Download the google-services.json and GoogleService-info.plist and place them in their appropriate folders
Also, _auth.signInWithEmailAndPassword(email: email, password: "1") is a async process and takes time to complete.
You need to have await

This appears to be an issue with how errors are thrown and caught when dealing with async, FlutterFire hasn't gotten around to putting theirs into try/catch yet - which we can't do anything about.
Here they discuss it: https://github.com/FirebaseExtended/flutterfire/issues/3475
Here is a pending fix: https://github.com/FirebaseExtended/flutterfire/pull/3700
At the moment the answer seems to be essentially to either step through the exceptions, or uncheck 'Uncaught exceptions' in the debugger (which is not great since it won't catch actual exceptions..)

Related

Flutter Firebase OTP verification fails even though both sent and entered OTP are the same

I am trying to implement OTP verification in my Flutter app using Firebase, but I am facing an issue where the verification fails even though both the sent and entered OTP are the same.
I am using the following function to verify the OTP:
void verifyOtp({
required BuildContext context,
required String verificationId,
required String userOtp,
required Function onSuccess,
}) async {
_isLoading = true;
notifyListeners();
try {
PhoneAuthCredential creds = PhoneAuthProvider.credential(
verificationId: verificationId, smsCode: userOtp);
User? user = (await _firebaseAuth.signInWithCredential(creds)).user!;
print('signwithcredential passed');
if (user != null) {
_uid = user.uid;
onSuccess();
}
} on FirebaseAuthException catch (e) {
print('failed Userotp: $userOtp');
showSnackBar(context, e.message.toString());
_isLoading = false;
notifyListeners();
}
}
The error I am getting is "FirebaseAuthException: sms code has expired please resend the verification code to verify again."
I am new to Flutter and Firebase, so any help in fixing this problem would be greatly appreciated.
Here are couple of things I have found maybe they will work:
Check the Firebase Console to ensure that the phone number being verified is correctly set up and that the SMS code is being sent to the right number.
Make sure that the phone number is being passed in the correct format.
FirebaseAuthException is being caught correctly. Make sure the message inside the catch block is being logged or displayed to the user.
Ensure that the dependencies and packages are correctly installed and imported.
The solution is itself an error you are getting. You have to enter the code before it expire. Try to verify the otp as soon as you get the code as it's saying "I/flutter ( 9598): error on firebaseAuthexception: [firebase_auth/session-expired] The sms code has expired. Please re-send the verification code to try again. ".

Flutter firebase: reauthentication

I have a section of an app dedicated to deleting all of a user's data. Firebase auth requires a user to reathenticate before deleting its data, so im trying the following function:
onPressed: () async {
try {
AuthCredential
credential =
EmailAuthProvider
.credential(
email: '',
password: '',
);
_firebaseAuth.currentUser!
.reauthenticateWithCredential(
credential);
BlocProvider.of<
ProfileBloc>(
context)
.add(DeleteProfile(
user: user,
stingrays: stingrayState
.stingrays,
));
Navigator.pushNamed(
context, '/wrapper/');
} on Exception catch (e) {
print(e.toString());
}
},
In this case, setting the email and password as '' should throw an error. My current idea is that the function should run until reauthenciation is called, then an error should be thrown and it does not finish the deleting process. However, it currently does not operate like this, instead running through the entire function. Any idea how to fix this conditional reauthentication method?
Thank you!
You can just call return whenever you want your code to stop and it will.
So after your authentication do this.
_firebaseAuth.currentUser!.reauthenticateWithCredential(credential);
return;
Also reauthenticateWithCredential() is a Future<void> so you have to await it:
return await _firebaseAuth.currentUser!.reauthenticateWithCredential(credential);

Why am I not getting a password reset email from the Firebase service?

I am trying to implement a password reset function that incorporates a SnackBar to display either success or error messages. The code shown below produces both the success message and the various error messages, as appropriate - but I never receive the password reset email from the Firebase service. I'm not sure if it's a Firebase setup issue or an issue with the code.
Future resetPassword() async {
try {
await FirebaseAuth.instance
.sendPasswordResetEmail(email: _emailController.text.trim());
_showSnackBar('Password reset link sent');
} on FirebaseAuthException catch (e) {
_showSnackBar(e.message.toString());
return;
}
}
Future<void> _showSnackBar(String msg) async {
final snackBar = SnackBar(
content: Text(msg),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
Disregard. Code seems to work fine - problem was either with Firebase Auth service or my internet service provider last night. All good now.
Make sure to check your spam, because if did not add authorize rules in firebase then the reset link goes in spam mail.
.

Flutter firebase auth throwing platform exception not being caught by try catch

Im running into this issue and im not sure why. I have everything set up on firebase and everything works fine for signup up and signin when you type the username/pass correctly. However, when trying to catch error, im getting a platform exception and its in a wierd form. (This is me trying an incorrect password). As you can see theres a Firebaseauthexception inside a Platform Exception inside a platform exception and my try catch isnt even able to catch it. Im updated to the most recent version of ^1.1.1
PlatformException (PlatformException(firebase_auth, com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The password is invalid or the user does not have a password., {code: wrong-password, additionalData: {}, message: The password is invalid or the user does not have a password.}, null))
try {
userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(email: user, password: pass);
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
print('No user found for that email.');
} else if (e.code == 'wrong-password') {
print('Wrong password provided for that user.');
}
}
UPDATE im also getting this displayed:
Ignoring header X-Firebase-Locale because its value was null.
UPDATE 2
found this link which describes what im having similarly
Google Sign-In With Flutter: Error Code -4
Any insight would be greatly appreciated

Flutter How can I check internet connection through the whole app NOT only in one particular Class, and prompt pop-up dialog when it lost?

How can I continuously check internet connection for whole application(I mean all classes and widgets) and prompt the pop-up dialog when the connection is lost. Please provide an example if it is possible.
You need to use the Connectivity Plugin.
import 'dart:io';
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
print('connected');
}
} on SocketException catch (_) {
print('not connected');
}