callum norris
callum norris

Reputation: 33

Flutter: Creating a user in Firestore using the account created from Firebase Authentication

I am creating a flutter app using dart language with Firebase for authentication.

My code for a user signing up is

  Future<void> signUp() async {
if(_formKey.currentState.validate()){
  _formKey.currentState.save();
  try{
    AuthResult user = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: _email, password: _password);

    Navigator.of(context).pop();
    Navigator.pushReplacement(context,MaterialPageRoute(builder: (context)=> LoginPage()));
  }catch(e){
    print(e.message);
  }

Does anyone know how I would use that newly created users credentials to create an entry in the database for user?

I have set up the database with a dummy user which I manually entered but would like to be able to create entries from a user signing up.

Upvotes: 3

Views: 9765

Answers (2)

Karen
Karen

Reputation: 1569

You can get the uid from AuthResult to create a document in Firestore. setData updates the document or in this case, creates one because it doesn't exist. Make sure to install cloud_firestore_package.

UserCredential result = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: _email, password: _password);
User user = result.user
await FirebaseFirestore.instance.collection('users')
      .doc(user.uid).set({ 'firstName': _firstName})

Upvotes: 13

vitooh
vitooh

Reputation: 4262

First you need to connect to database. To do this you have to add cloud_firestore package to your project.

On the same page you might find very nice example how to do it in flutter.

If you seek for more details on the same package page you may find API reference.

Just study this package, everything should be there.

I hope it will help! Good luck!

Upvotes: 0

Related Questions