Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Document for each user?

I am wondering how to make a document for each user as they create their account (with Firebase Web). I have Firebase Authentication enabled and working, and I'd like each user then to have a document in Cloud Firestore in a collection named users. How would I get the UID and then automatically create a document for each user? (I am doing this so that calendar events can be saved into an array field in the document, but I need a document for the user to start with). I am aware and know how to make security rules for access, I just don't know how to make the document in the first place. Thanks!

like image 944
NeuronButter Avatar asked Apr 29 '18 12:04

NeuronButter


People also ask

Can two documents have the same ID in firestore?

New document with same ID should not be allowed in the same collection. It should be possible to fetch an already existing document from a previous import.

How many documents can be in a collection Firebase?

10 for single-document requests and query requests. 20 for multi-document reads, transactions, and batched writes. The previous limit of 10 also applies to each operation.

Can a document contain another collection in Firebase?

A collection contains documents and nothing else. It can't directly contain raw fields with values, and it can't contain other collections.


2 Answers

While it is definitely possible to create a user profile document through Cloud Functions, as Renaud and guillefd suggest, also consider creating the document directly from your application code. The approach is fairly similar, e.g. if you're using email+password sign-in:

firebase.auth().createUserWithEmailAndPassword(email, password)
  .then(function(user) {
    // get user data from the auth trigger
    const userUid = user.uid; // The UID of the user.
    const email = user.email; // The email of the user.
    const displayName = user.displayName; // The display name of the user.

    // set account  doc  
    const account = {
      useruid: userUid,
      calendarEvents: []
    }
    firebase.firestore().collection('accounts').doc(userUid).set(account); 
  })
  .catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    // ...
  });

Aside from running directly from the web app, this code also creates the document with the user's UID as the key, which makes subsequent lookups a bit simpler.

like image 82
Frank van Puffelen Avatar answered Sep 23 '22 16:09

Frank van Puffelen


You´ll have to set a firebase function triggered by the onCreate() Auth trigger.
1. create the function trigger
2. get the user created data
3. set the account data.
4. add the account data to the collection.

functions/index.js

// Firebase function 

exports.createAccountDocument = functions.auth.user().onCreate((user) => {
  // get user data from the auth trigger
  const userUid = user.uid; // The UID of the user.
  //const email = user.email; // The email of the user.
  //const displayName = user.displayName; // The display name of the user.

  // set account  doc  
  const account = {
    useruid: userUid,
    calendarEvents: []
  }
  // write new doc to collection
  return admin.firestore().collection('accounts').add(account); 
});
like image 38
guillefd Avatar answered Sep 25 '22 16:09

guillefd