i would like to change the auto generated document ID in Firestore to the user phone number, also when user register i need to check if the phone number exist so no duplication happens, this is what i tried to do when user hit the sign up button but the id still auto generated:
void signUp(String email, String password) async {
if (_formKey.currentState!.validate()) {
await _auth
.createUserWithEmailAndPassword(email: email, password: password)
.then((value) => {postDataToFirestore()})
.catchError((e) {
Fluttertoast.showToast(msg: e!.message);
});
}
}
postDataToFirestore() async {
//Creating Instance of firestore from firebase
FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;
User? user = _auth.currentUser;
//Creating an instance of UserModel Class
UserModel userModel = UserModel();
//providing the fields values to the user model class
userModel.uid = phoneController.text;
userModel.name = nameController.text;
userModel.email = emailController.text;
userModel.phone = phoneController.text;
userModel.province = dropDownValue;
userModel.dateOfBirth = dateController.text;
userModel.challengeHistory = [];
userModel.challengeMember = [];
userModel.profileImage =
'https://firebasestorage.googleapis.com/v0/b/athaddak-bf02c.appspot.com/o/default_picture/Blank-Avatar.png?alt=media&token=47255868-37a0-4a55-8bc4-1d1ef5599ded';
userModel.challenges = 0;
userModel.winnings = 0;
userModel.events = 0;
userModel.points = 20;
//Sending data to firestore
await firebaseFirestore
.collection("Users")
.doc(user!.uid)
.set(userModel.toMap());
//Account Created Successfuly message
Fluttertoast.showToast(msg: "Account Created Successfully .. :)");
Navigator.pushAndRemoveUntil(
(context),
MaterialPageRoute(builder: (context) => HomeScreen()),
(route) => false);
}
and this is the phone number field if the validation of duplication happens here am not sure how:
final phoneField = TextFormField(
controller: phoneController,
keyboardType: TextInputType.phone,
autofocus: false,
autocorrect: false,
textInputAction: TextInputAction.next,
validator: (value) {
if (value!.isEmpty) {
return "Please Enter a Phone Number";
}
},
onSaved: (value) {
phoneController.text = value!;
},
textAlign: TextAlign.center,
decoration: InputDecoration(
prefixIcon: Icon(Icons.phone),
contentPadding: EdgeInsets.fromLTRB(20, 15, 20, 15),
hintText: AppLocalizations.of(context)!.phoneNumber,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
);
to summarize:
1- how to change document ID in Firestore to user phone number.
2- When user register how to check for duplicated phone number.
i would really be thankful for any help.
CodePudding user response:
Your code completely ignores the telephone number when creating the user. Which is probably why it's not using the telephone number.
You write: await firebaseFirestore....doc(user!.uid)
user!.uid
is the UID from Firebase Authentication (which also uses a unique ID). So you are creating a user with the same UID as the Firebase Authentication.
If you wanted to use the telephone number as a UID, then you should create the user with that UID. ie. instead of await firebaseFirestore....doc(user!.uid)
you'll need await firebaseFirestore....doc(userModel.uid)
perhaps.
BTW. I think this is a very, very bad idea. Your telephone number has to be the right format, it has to confirm to Firebase field character restrictions, you'll need to canonicalise all different ways of writing the same telephone number, and you'll be in for a world of hurt when telephone numbers change etc.