I wrote a function for creating accounts in Firebase two months ago and it worked, now when I try to create an account it gives me the next error message:
I/BiChannelGoogleApi(19700): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzaq#94e857b
The function that I created for doing that stuff is the next one:
Future<void> register() async {
final formState = _formKey.currentState;
if (formState.validate()) {
formState.save();
print('$_email si $_password');
try {
FirebaseUser user = (await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: _email.trim(), password: _password, ))
.user;
user.sendEmailVerification();
presentError("Now you own an account", context);
await Future.delayed(Duration(seconds: 1));
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => LoginPageState(title: 'LoginPage')));
} catch (e) {
print(e);}
Maybe you don't have updated your FirebaseAuth Rules and the unsafe period is over.
See https://firebase.google.com/docs/rules/rules-and-auth
Related
I did the authentication, but when I click to create an account it throws an error:
MissingPluginException(No implementation found for method createUserWithEmailAndPassword on channel plugins.flutter.io/firebase_auth)
final _auth = FirebaseAuth.instance;
void _submitAuthForm(String email, String user, String pass, bool isLogin , context) async {
AuthResult authResult;
try {
if (isLogin) {
authResult = await _auth.signInWithEmailAndPassword(
email: email, password: pass);
} else {
authResult = await _auth.createUserWithEmailAndPassword(
email: email, password: pass);
}
} on PlatformException catch (err) {
var errorMessage = 'An error ocurred. Please check your credentials.';
if (err.message != null) {
errorMessage = err.message!;
}
showDialog(
context: context,
builder: (context) => AlertDialog(
content: Text(errorMessage),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('OK'),
),
],
),
);
} catch (error) {
print(error);
}
}
Make sure you have enabled Sign-in with Email/Password in Firebase Console.
Also, try updating packages to the latest version. Along with that do upgrade your flutter version as well.
Once you did all that, try running flutter clean and re-running your project.
Cheers!! Hope it works.
Solution:
stop the app
run the app again
Hope it helps!
I have an app which worked well some days back. I'm trying to insert data into the realtime database, but the data is not not inserting, and I don't get any error. My rule is set to true
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
DatabaseReference usersRef =
FirebaseDatabase.instance.reference().child("users");
registerUser(BuildContext context) async {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return ProgressDialog(msg: 'Authenticating');
});
final User firebaseUser = (await _firebaseAuth
.createUserWithEmailAndPassword(
email: emailController.text.trim(),
password: passwordController.text.trim())
.catchError((errorMsg) {
Navigator.pop(context);
displayToastMsg("Error: " + errorMsg.toString(), context);
}))
.user;
if (firebaseUser != null) {
Map userDataMap = {
"name": nameController.text.trim(),
"phone": phoneController.text.trim(),
"email": emailController.text.trim(),
};
usersRef.child(firebaseUser.uid).set(userDataMap);
displayToastMsg('Account created successfully', context);
// Navigator.pushNamedAndRemoveUntil(
// context, MainScreen.routeScreen, (route) => false);
} else {
Navigator.pop(context);
displayToastMsg('User was not created', context);
}
}
Is there something I'm missing?
Are you running FirebaseApp.configure in the main file? Assuming the database url is correct in the configure method you should be able to create a reference by => usersRef = FirebaseDatabase.instance.reference().child('users/${firebaseUser.uid}'); and then
upload the userMap by usersRef.set(userDataMap).
If I try to fill TextFormFields responsible for email and password and submit, the login procedure gonna be success always.. no matter if user is in firebase authentication field. I've done login and register pages before with firebase working perfectly. Only diffrence was using pushNamed instead pushReplace. Should it do difference?
That's form inkwell onTap :
onTap: () {
final formState = _formkey.currentState;
if (formState.validate()) {
formState.save();
signIn(_email, _password).whenComplete(
() => Navigator.of(context)
.pushReplacement(
MaterialPageRoute(
builder: (context) =>
HomePage(),
)));
}
}
and authentication:
FirebaseAuth _auth = FirebaseAuth.instance;
Future<void> signIn(String _email, String _password) async {
try {
UserCredential user = await _auth.signInWithEmailAndPassword(
email: _email, password: _password);
} on FirebaseAuthException catch (e) {
print('error');
}
}
Email/password sign-in method is turn on.
How to fix this problem and allow to log in only users that exist in firebase?
i'm new with flutter and i want to implement a log in screen on my application.
for that i use flutter and firebase.
the problem is that when anything in the email and password field the application access to the home.
so i need to verify the email before get the access.
here is the code
thanks guys for help me,
onPressed: () {
if (_formKey.currentState.validate()) {
Future<String> check = logIn();
if (check != null) {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (BuildContext context) =>
HomePage()));
}
}
Firebase offers a function for checking if users are verified which I used in the code below.
Future<bool> login(
String email, String password) async {
final user = (await FirebaseAuth.instance
.signInWithEmailAndPassword(email: email, password: password))
.user;
if (user.isEmailVerified) {
return true;
}
return false;
}
However, to use this function, you have first send an email verification to your users somewhere in the function you call when your users create an account. The function below will create the user and send a verification email and then return a FirebaseUser when done.
Future<FirebaseUser> register() async {
await _auth
.createUserWithEmailAndPassword(email: email.trim(), password: password)
.then(
(result) async {
//send verifcation email
result.user.sendEmailVerification();
return result.user;
},
);
return null;
}
In my app, after authentication, user can move to the next screen.
signUpWithEmail().then((user) {
Navigator.push(context,
MaterialPageRoute(builder: (context) {
return HomePage();
}));
}).catchError((error) {
print("THE ERROR : $error");
});
Now signUpWithEmail may fail for various reasons like : invalid e-mail, internet connectivity failure and so on. How can I detect those errors and prevent navigation? Here is signUpWithEmail() method:
Future<FirebaseUser> signUpWithEmail() async {
String email = emailControlller.text;
String password = passwordControlller.text;
FirebaseUser user = await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: emailControlller.text,
password: passwordControlller.text,
)
.then((user) {
// set balance to 0
Firestore.instance
.collection("users")
.document(user.uid)
.setData({"cash": 0});
}).catchError((e) => print("error : $e"));
return user;
}
You is returning to signUpWithEmail() of anyway, you don't throw the error, so it never will enter on
.catchError((error) {
print("THE ERROR : $error");
})
To fix it you must throw the error on your signUpWithEmail(). Try something like it.
Future<FirebaseUser> signUpWithEmail() async {
String email = emailControlller.text;
String password = passwordControlller.text;
FirebaseUser user = await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: emailControlller.text,
password: passwordControlller.text,
)
.then((user) {
// set balance to 0
Firestore.instance
.collection("users")
.document(user.uid)
.setData({"cash": 0});
}).catchError((e) => {
print("error : $e")
throw("Your error") // It return to catch
});
return user;
}
Let me know if you can make it.
Use FutureBuilder widget and wrap the logic in the builder method