Not able to catch error of changing Firebase User email - firebase

Since hours I have an Problem with my method. When trying to update use email and the email really exists its works fine. But when I pasting just any sh-- its giving me an error in console but not returning anything . Please help
This is my method
//update in with passwort and email
Future updateemail2(String email, String password, String newemail) async {
try {
UserCredential authResult =
await _auth.currentUser.reauthenticateWithCredential(
EmailAuthProvider.credential(
email: email,
password: password,
),
);
User user = authResult.user;
if (user != null) {
user.verifyBeforeUpdateEmail(newemail);
// user.sendEmailVerification();
}
// user?.emailVerified;
} on FirebaseAuthException catch (e) {
switch (e.code) {
case 'invalid-email':
{
return 'Email ist nicht gültig';
}
case 'email-already-in-use':
{
return 'Email bereits registriert';
}
case 'wrong-password':
{
return 'Passwort ist nicht gültig';
}
case 'internal-error':
{
return "internal error";
}
default:
{
return 'Unexpected Error';
}
return null;
}
}
return null;
}
Im calling it like that
buildupdate(context, userData, user) async {
if (_formKey.currentState.validate()) {
String authError4 = await _auth.updateemail2(
userData.email, userData.password, _currentemail);
if (authError4 != null) {
setState(() {
showerror = true;
error = authError4;
});
} else {
print("halts maul");
}
This is the error which im not able to catch

Related

How to update user email and catch error?

I'm using the following method to update user email. But the problem that I have with that was first that I could not catch errors from user.updateEmail. So when ever there was an error it was not returning it and the else statement was true. But in console it was giving me the correct error like....invalid-email... But then suddenly the function did not work anymore at all!. SO I have no idea what the problem was with catching the error of updateemail method and now getting another error which says:
GTMSessionFetcher invoking fetch callbacks, data {length = 204, bytes = 0x7b0a2020 22657272 6f72223a 207b0a20 ... 5d0a2020 7d0a7d0a }, error Error Domain=com.google.HTTPStatus Code=400 "(null)" UserInfo={data={length = 204, bytes = 0x7b0a2020 22657272 6f72223a 207b0a20 ... 5d0a2020 7d0a7d0a }, data_content_type=application/json; charset=UTF-8}
String authError4 = await _auth.updateemail(
userData.email, userData.password, _currentemail);
if (authError4 != null) {
setState(() {
showerror = true;
error = authError4;
});
} else {
//update in with password and email
Future updateemail(String email, String password, String newemail) async {
try {
//if (FirebaseAuth.instance.currentUser.emailVerified) {print('fuckyooooooo');
print(email);
print(password);
UserCredential authResult =
await _auth.currentUser.reauthenticateWithCredential(
EmailAuthProvider.credential(
email: email,
password: password,
),
);
User user = authResult.user;
if (user != null) {
user.updateEmail(newemail);
}
// user?.emailVerified;
} on FirebaseAuthException catch (e) {
switch (e.code) {
case 'invalid-email':
{
return 'Email ist nicht gültig';
}
case 'email-already-in-use':
{
return 'Email bereits registriert';
}
case 'wrong-password':
{
return 'Passwort ist nicht gültig';
}
default:
{
return 'Unexpected Error';
}
return null;
}
}
return null;
}
So hope anyone can help with that. Just to make it clear my questions are: How to catch errors of update email? and What is the new error about and how to solve it?
Edit:
I might have solved the first Problem GTMSessionFetcher. I think it happened because I sent too many requests.

Flutter User creation and Signing in

Here is my code
I am trying to run this code with no avail.
import 'package:firebase_auth/firebase_auth.dart';
class AuthClass {
FirebaseAuth auth = FirebaseAuth.instance;
//create account
Future<String> createAccount(
{required String email, required String password}) async {
try {
await auth.createUserWithEmailAndPassword(
email: email, password: password);
return "Account created";
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
return 'The password provided is too weak.';
} else if (e.code == 'email-already-in-use') {
return 'The account already exists for that email.';
}
} catch (e) {
return 'Error Occured';
}
}
//sign in user
Future<String> signIn(
{required String email, required String password}) async {
try {
await auth.signInWithEmailAndPassword(email: email, password: password);
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
return 'No user found for that email.';
} else if (e.code == 'wrong-password') {
return 'Wrong password provided for that user.';
}
}
}
// reset password
Future<String> resetPassword({
required String email,
}) async {
try {
await auth.sendPasswordResetEmail(
email: email,
);
return 'Email Sent';
} catch (e) {
return 'Error Occured';
}
}
//sign out
void signOut() {
auth.signOut();
}
}
I need some help I am getting this error,
The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type.
Try adding either a return or a throw statement at the end.
Future signIn(...) "signIn"
Future createAccount(...) "createAccount"
is where i am getting the error
try on catch blocks work like this.
try executes code. If that fails, it throws an Exception.
Now, that Exception can be of many types and using on and catch together we can catch a specific exception and get that value inside e.
But the on FirebaseAuthException catch (e), only catches Exceptions of type FirebaseAuthException.
So, in a case where your try block throws something else, maybe NumberException, then it won't be caught.
For catching all other types of Exceptions, you need to add another catch block.
Like this,
try {
await auth.signInWithEmailAndPassword(email: email, password: password);
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
return 'No user found for that email.';
} else if (e.code == 'wrong-password') {
return 'Wrong password provided for that user.';
}
} catch (e) {
return 'Unknown Error.'; // Change this to whatever you want.
}
Think of the try like an if block. Then your on FirebaseAuthException catch (e) would be an if else block. But you also need to handle the else block and that is your catch (e).
Hope it makes sense.

Flutter Firebase try catch does not fire

When using firebase auth and type not registered email,
although I used try - catch, that does not fire.
fAuth.signInWithEmailAndPassword definitely throw PlatformException but that code does not catch that.
I suffered this problem about 2days..... please help me
Future<bool> signInWithEmail(String email, String password) async {
try {
var result = await fAuth.signInWithEmailAndPassword(
email: email, password: password);
if (result != null) {
setUser(result.user);
FirebaseFirestore.instance.collection("User").doc(_user.uid).get().then((doc) {
info.set(
name: doc["name"],
email: doc["email"],
phoneNumber: doc["phoneNumber"],
follower: doc["follower"],
following: doc["following"],
like: doc["like"],
review: doc["review"],
visited: doc["visited"],
favorite: doc["favorite"],
);
print(doc['name']);
notifyListeners();
});
return true;
}
return false;
} on PlatformException catch (e) {
List<String> result = e.toString().split(", ");
setLastFBMessage(result[1]);
return false;
} on Exception catch (e) {
List<String> result = e.toString().split(", ");
setLastFBMessage(result[1]);
return false;
} catch (e) {
List<String> result = e.toString().split(", ");
setLastFBMessage(result[1]);
return false;
}
}

How check if user verify mail and then let logged in?

I know the question was asking a lot of times but after spending hours trying to understand im still don't know how to doing that.
So right know I got 2 Future methods
the SigIn
Future<String> signIN(String email, String password) async {
try {
(await FirebaseAuth.instance.signInWithEmailAndPassword(
email: email.trim(),
password: password,
))
.user;
} on FirebaseAuthException catch (e) {
switch (e.code) {
case 'invalid-email':
{
return 'Email is not valid';
}
case 'user-disabled':
{
return 'Account is not active';
}
case 'user-not-found':
{
return 'No user found';
}
case 'wrong-password':
{
return 'wrong password';
}
default:
{
return 'Unexpected error!';
}
}
}
return null;
}
And the Sign up
Future<String> signUp(String email, String password) async {
try {
(await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: email.trim(),
password: password,
))
.user
.sendEmailVerification();
} on FirebaseAuthException catch (e) {
switch (e.code) {
case 'invalid-email':
{
return 'Email is not valid';
}
case 'user-disabled':
{
return 'Account is not active';
}
case 'user-not-found':
{
return 'No user found';
}
case 'wrong-password':
{
return 'wrong password';
}
default:
{
return 'Unexpected error!';
}
}
}
return null;
}
And i wish I know how to first check if user verified mail and then let him login.
Right know user press register button and automatically login .
Please help!!
Here the on pressed methods
First the pressed register button
onPressed: () async {
if (_formKey.currentState.validate()) {
String authError = await _auth.signUp(email, password);
if (authError != null) {
setState(() => jawoll = true);
setState(() => error = authError);
}else{
setState(() => loading = true);
setState(() => jawoll = false);
setState(() => error = "Email send to $email");
}
}
}
And the sign in in button
onPressed: () async {
if (_formKey.currentState.validate()) {
String authError = await _auth.signIN(email, password);
if (authError != null) {
setState(() => error = authError);
print("olaaa");
print(error);
}
setState(() => loading = false);
}
}
Remove the stream (authStateChanges) from your widget tree that is responsible for automatically logging in the user.
Then, manually push a new screen for email registration. After it's done let your user go to the home screen.
On opening the app again in the future, you can check if the user is logged in and if the email is verified. If so, move to the home screen and if not to the log in screen.
The problem here is that the stream automatically changes on user login, wether the email is verified or not. So, remove it and proceed !!

Flutter Firestore adding data

I want to add data on firestore and it wont work. can somebody help me.
This is the newest updated version and I can't figure out how...
firebase_auth: ^0.18.0+1 cloud_firestore: ^0.14.0+2
This is the sign up screen so I want to send data after I create the email and password.
I want to add the document with user uid too.
onPressed: () async {
try {
UserCredential userCredential = await FirebaseAuth
.instance
.createUserWithEmailAndPassword(
email: _emailController.text,
password: _passwordController.text,
);
if (userCredential != null) {
firestore
.collection("user")
.doc('user.uid')
.set({
'username': username,
'email': email,
})
.then((value) => print("User Added"))
.catchError((error) =>
print("Failed to add user: $error"));
Navigator.of(context).pushNamed(AppRoutes.authLogin);
}
} catch (e) {
print(e);
_usernameController.text = "";
_passwordController.text = "";
_repasswordController.text = "";
_emailController.text = "";
//TODO: alertdialog with error
}
setState(() {
saveAttempted = true;
});
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
}
},
Can someone help me with the firestore.. Thank you..
First Create a User class.
class UserData {
final String userId;
final String fullNames;
final String email;
final String phone;
UserData(
{this.userId,
this.fullNames,
this.email,
this.phone});
Map<String, dynamic> getDataMap() {
return {
"userId": userId,
"fullNames": fullNames,
"email": email,
"phone": phone,
};
}
}
Then you can use a function like this one to save the credentials and save the data to firestore
createOrUpdateUserData(Map<String, dynamic> userDataMap) async {
FirebaseUser user = await FirebaseAuth.instance.currentUser();
DocumentReference ref =
Firestore.instance.collection('user').document(user.uid);
return ref.setData(userDataMap, merge: true);
}
==
bool validateAndSave() {
final form = _formKey.currentState;
if (form.validate()) {
form.save();
return true;
}
return false;
}
void validateAndSubmit() async {
if (validateAndSave()) {
try {
String userId = _formType == FormType.login
? await widget.auth.signIn(_email, _password)//use your signin
: await widget.auth.signUp(_email, _password);//use your signup
if (_formType == FormType.register) {
UserData userData = new UserData(
fullNames: _fullNames,
email: _email,
phone: "",
);
createOrUpdateUserData(userData.getDataMap());
}
} catch (e) {
setState(() {
_isLoading = false;
switch (e.code) {
case "ERROR_INVALID_EMAIL":
_authHint = "Your email address appears to be malformed.";
break;
case "ERROR_EMAIL_ALREADY_IN_USE":
_authHint = "Email address already used in a different account.";
break;
case "ERROR_WRONG_PASSWORD":
_authHint = "Your password is wrong.";
break;
case "ERROR_USER_NOT_FOUND":
_authHint = "User with this email doesn't exist.";
break;
case "EMAIL NOT VERIFIED":
_authHint = "Email not verified: Please go to yor email and verify";
break;
case "ERROR_USER_DISABLED":
_authHint = "User with this email has been disabled.";
break;
case "ERROR_TOO_MANY_REQUESTS":
_authHint =
"Too many Attemps. Account has temporarily disabled.\n Try again later.";
break;
case "ERROR_OPERATION_NOT_ALLOWED":
_authHint = "Signing in with Email and Password is not enabled.";
break;
case "ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL":
_authHint = "The email is in use by another account";
break;
default:
_authHint = "An undefined Error happened.";
}
});
print(e);
errorDialog(context, _authHint);
}
} else {
setState(() {
_authHint = '';
});
}
}
Then use
onpressed:(){
validateAndSubmit();
}
the formtype is an Enum
enum FormType { login, register, reset }
widget.auth.signIn and widget.auth.signUp should be replaced with your signin and signup respectively.
Added a custom error block to differentiate firebase auth errors as well.
Defining an auth page independently will help you reuse your code in future.

Resources