Catching errors in Flutter / Firebase Auth sign up - firebase

I'm having a bit of trouble catching errors from firebase authentication instances in flutter , I don't know what is wrong the code still throws exceptions even though i'm catching the errors , In addition , I have no idea how to identify the exception type whether its a badly formatted email , email already in use , weak password , etc.... and there is no proper documentation for such thing
I have tried this:
FirebaseAuth.instance.createUserWithEmailAndPassword(
email: _email, password: _password)
.then((currentUser) => {
//Execute
}).catchError((onError)=>{
print(onError)
});
And tried a simple try-catch block and none of them catch the exception

FirebaseAuth.instance.createUserWithEmailAndPassword(
email: _email, password: _password)
.then((currentUser) => {
//Execute
}).catchError((onError)=>{
//Handle error i.e display notification or toast
});
This code actually works but the editor (Visual Studio code) itself is throwing exceptions and crashing the app , if you run the app from the device or emulator itself the issue will be solved

You need to import - import 'package:flutter/services.dart';
then try this code:
FirebaseAuth.instance
.createUserWithEmailAndPassword(email: _email, password: _password)
.catchError((onError) => print(onError.message))
.then((authResult) => {
//Execute
})

Related

React Native, Firebase: Error (auth/admin-restricted-operation) fix?

const handleSignUp = () => {
createUserWithEmailAndPassword(auth, email, password)
.then((re) => {
console.log(re);
})
.catch((error) => {
console.log(error);
}
);
};
This is the signup function that I've implemented for email and password authentication using Firebase authentication. The error that I'm getting is,
Firebase: Error (auth/admin-restricted-operation).
This happens every time I run this function and the firebase authentication tab does not show the new user. Been stuck on this error for quite a while now and am unable to fix it. Any help would be highly appreciated.

How to enable firebase phone verification in flutter web ? I have tried many ways but it doesn't seems to work, SMS code need to be sent to the user

I want to implement the phone verification by sending OTP using firebase, how to do this? . I have tried by following this thread in github, but no use it's not helping, signInwithphoneNumber only signs in user not verifies the user by sending SMS OTP code, how to solve this?
Github thread link:https://github.com/flutter/flutter/issues/46021
When I have implemented throwed the following error:
Error: UnimplementedError: verifyPhoneNumber() is not supported on the web. Please use
`signInWithPhoneNumber` instead.
Can someone help me out please !!!
You have to use
FirebaseAuth auth = FirebaseAuth.instance;
// Wait for the user to complete the reCAPTCHA & for an SMS code to be sent.
ConfirmationResult confirmationResult = await auth.signInWithPhoneNumber('+44 7123 123 456');
then this to verify
UserCredential userCredential = await confirmationResult.confirm('123456');
You can also add RecaptchaVerifier as per your own use like
ConfirmationResult confirmationResult = await auth.signInWithPhoneNumber('+44 7123 123 456', RecaptchaVerifier(
container: 'recaptcha',
size: RecaptchaVerifierSize.compact,
theme: RecaptchaVerifierTheme.dark,
));
and you can also edit reCaptcha
RecaptchaVerifier(
onSuccess: () => print('reCAPTCHA Completed!'),
onError: (FirebaseAuthException error) => print(error),
onExpired: () => print('reCAPTCHA Expired!'),
);

How to handle exception while Re-Registering with Existing email and password in Firebase Flutter?

I made a flutter app and connected it with Firebase authentication (using email and password). It's working perfectly. It creates a new user when a user register into app, signs out, and also signs in with that email and password.
But, when I try to Re-Register with the same email and password that I've used to create before OR when I try to Sign in with an email which is not registered, I get this error:
PlatformException (PlatformException(firebase_auth, com.google.firebase.FirebaseException: An internal error has occurred. [ Unable to resolve host "www.googleapis.com":No address associated with hostname ], {}, null))
And the app just stops working, like in this case the loading is stopped: See the image here
This is the function to Register, SignIn function is similar to this:
Future registerWithEmailAndPasswordWithFirebase(
String email, String password) async {
try {
UserCredential credential = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
User user = credential.user;
await DatabaseService(uid: user.uid).updateUserData('New user');
return _returnUserUid(user);
} catch (e) {
print(e.toString());
return null;
}
}
How can I resolve this?
In the above case, I ran the app using Vscode debugging. So, whenever it was getting an error, the app just stops there and vscode shows the PlatformException.
When I ran my app from terminal, then this issue didn't occur, it just shows that PlatformException in the terminal only and the app keep running.

Unable to reset email or password due to Firebase site not found problem

I recently attempted to implement the ability to reset a user's email and password in my Flutter application. It sent the email successfully but shortly after I clicked on the URL link I was met with a "Site Not Found". Multiple options on why this message was being displayed were listed below but they had to do with Firebase Hosting or using a custom domain which I use neither of them. I viewed multiple Youtube tutorials to see if they ran into similar problems but the link took them to a elegantly formatted website where they were able to reset their email or password. I am not sure why this problem is occurring but it might be due to the fact that I need a custom domain, although none of the Youtubers required it.
Below is the error I'm faced with on the site:
Below is the Flutter code:
Firebase Auth:
firebase_auth: ^0.17.0-dev.2
Reset password:
Future<void> resetPassword(String email) async {
await _authInstance.sendPasswordResetEmail(email: email);
}
Reset email:
Future<void> resetEmailAddress(
String newEmail, String oldEmail, String password) async {
var authResult = await _authInstance.signInWithEmailAndPassword(
email: oldEmail, password: password);
await authResult.user.updateEmail(newEmail);
await _instance
.collection('users')
.doc(authResult.user.uid)
.update({'email': newEmail});
}
Lastly I'm sorry for the lack of error checking and thank you for you time!
Matt

Meteor createUser callback only works on web

Accounts.createUser({
email: email,
password: password
}, function (err) {
console.log("inside createUser callback");
if (err) {
console.log("createUser failed", err);
return false;
}
console.log("callback inside createUser");
IonModal.close();
});
I am developing an ios app with Meteor (and Meteor Ionic), and I am stuck at a weird point. The callback for createUser only seems to be called when I test on my browser. In my iOS device, everything works except the callback. I don't see any error message.
Does anyone have an idea or have had a similar problem?

Resources