Firebase Document for each user?

2020-07-26 16:11发布

问题:

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!

回答1:

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.



回答2:

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); 
});