Reputation: 179
I am trying to authenticate newly registered users from a sign-up page and add them to my cloud-firestore database once they are authenticated. At the moment, the users are authenticated and the code works fine up to that stage. However, they are not added to the cloud-firestore database. Please help!
Container(
width: 150.0,
child: FlatButton(
child: Text('Sign Up'),
color: Colors.grey,
onPressed: () {
if (_registerFormKey.currentState.validate()) {
if (pwdInputController.text ==
confirmPwdInputController.text) {
FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: emailInputController.text,
password: pwdInputController.text)
.then((currentUser) => Firestore.instance
.collection("users")
.document(currentUser.user.uid)
.setData({
"uid": currentUser.user.uid,
"first-name":
firstNameInputController.text,
"last-name":
lastNameInputController.text,
"email": emailInputController.text,
'password': pwdInputController.text,
})
.then((result) => {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) =>
MyApp()),
(_) => false),
firstNameInputController.clear(),
lastNameInputController.clear(),
emailInputController.clear(),
pwdInputController.clear(),
confirmPwdInputController.clear()
})
.catchError((err) => print(err)))
.catchError((err) => print(err));
} else {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Error"),
content:
Text("The passwords do not match"),
actions: <Widget>[
FlatButton(
child: Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
}
}
},
),
),
Upvotes: 0
Views: 72
Reputation: 2654
Your code runs fine on my emulator so I would double check the following make sure you have set up firestore properly Documentation can be found here. Also double check your security rules for your datatbase Documentation for security rules can be found here.
if your database if still in testing phase your rules would look something like this:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// This rule allows anyone on the internet to view, edit, and delete
// all data in your Firestore database. It is useful for getting
// started, but it is configured to expire after 30 days because it
// leaves your app open to attackers. At that time, all client
// requests to your Firestore database will be denied.
//
// Make sure to write security rules for your app before that time, or else
// your app will lose access to your Firestore database
match /{document=**} {
allow read, write: if request.time < timestamp.date(2020, 5, 5);
}
}
}
Upvotes: 1