Firebase Document for each user?

后端 未结 2 903
青春惊慌失措
青春惊慌失措 2021-01-01 07:20

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

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-01 08:05

    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.

提交回复
热议问题