Add user displayname












0















How do I add a DisplayName in FireStore? I have read firebase no displayname for user and firebase user photourl and displayname and this but none of them work, since they all refer to Firebase and not Firestore.










share|improve this question





























    0















    How do I add a DisplayName in FireStore? I have read firebase no displayname for user and firebase user photourl and displayname and this but none of them work, since they all refer to Firebase and not Firestore.










    share|improve this question



























      0












      0








      0








      How do I add a DisplayName in FireStore? I have read firebase no displayname for user and firebase user photourl and displayname and this but none of them work, since they all refer to Firebase and not Firestore.










      share|improve this question
















      How do I add a DisplayName in FireStore? I have read firebase no displayname for user and firebase user photourl and displayname and this but none of them work, since they all refer to Firebase and not Firestore.







      firebase google-cloud-firestore






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 14:53









      Ishaan Javali

      1,3603820




      1,3603820










      asked Jan 2 at 5:02









      Hasan A YousefHasan A Yousef

      5,92943969




      5,92943969
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You can create a cloud function that triggers on account creation as such



          functions.auth.user().onCreate(event => {
          const firebaseUser = event.data;

          return firebase.firestore().collection("users").doc(firebaseUser.uid).set({
          name: firebaseUser.displayName || "n/a"
          });
          });


          That will create a document for the user in a collection called users, identified by its UID. Beware that the displayName is not necessarily set in the auth user object. So you might want to set a default placeholder that you can query for later and ask the user to provide information for.






          share|improve this answer































            0














            var config = {
            apiKey: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
            authDomain: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
            databaseURL: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
            projectId: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
            storageBucket: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
            messagingSenderId: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL"
            };
            firebase.initializeApp(config);
            // Initialize Cloud Firestore through Firebase
            var firestore = firebase.firestore();
            const settings = {/* your settings... */ timestampsInSnapshots: true};
            firestore.settings(settings);

            firebase.auth().onAuthStateChanged(function(user) {
            //check if user is signed in
            if (user) {
            //user is signed in
            //get user display
            if (user.displayName !== undefined) {
            var username = user.displayName;
            //add displayName to firestore
            firestore
            .collection("users")
            .doc(user.uid)
            .set({
            username: username
            }, {merge: true})
            .then(function() {
            alert('displayName added!');
            })
            .catch(function(e) {
            //there was an error writing to firestore
            //check console
            console.log(e);
            });
            } else {
            //user has not set displayName
            alert('no displayName');
            }
            }
            });





            share|improve this answer
























            • Appreciate your contribution and welcome stakoverflow, but I'm asking about firestore not firebase

              – Hasan A Yousef
              Jan 2 at 17:26











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001429%2fadd-user-displayname%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You can create a cloud function that triggers on account creation as such



            functions.auth.user().onCreate(event => {
            const firebaseUser = event.data;

            return firebase.firestore().collection("users").doc(firebaseUser.uid).set({
            name: firebaseUser.displayName || "n/a"
            });
            });


            That will create a document for the user in a collection called users, identified by its UID. Beware that the displayName is not necessarily set in the auth user object. So you might want to set a default placeholder that you can query for later and ask the user to provide information for.






            share|improve this answer




























              0














              You can create a cloud function that triggers on account creation as such



              functions.auth.user().onCreate(event => {
              const firebaseUser = event.data;

              return firebase.firestore().collection("users").doc(firebaseUser.uid).set({
              name: firebaseUser.displayName || "n/a"
              });
              });


              That will create a document for the user in a collection called users, identified by its UID. Beware that the displayName is not necessarily set in the auth user object. So you might want to set a default placeholder that you can query for later and ask the user to provide information for.






              share|improve this answer


























                0












                0








                0







                You can create a cloud function that triggers on account creation as such



                functions.auth.user().onCreate(event => {
                const firebaseUser = event.data;

                return firebase.firestore().collection("users").doc(firebaseUser.uid).set({
                name: firebaseUser.displayName || "n/a"
                });
                });


                That will create a document for the user in a collection called users, identified by its UID. Beware that the displayName is not necessarily set in the auth user object. So you might want to set a default placeholder that you can query for later and ask the user to provide information for.






                share|improve this answer













                You can create a cloud function that triggers on account creation as such



                functions.auth.user().onCreate(event => {
                const firebaseUser = event.data;

                return firebase.firestore().collection("users").doc(firebaseUser.uid).set({
                name: firebaseUser.displayName || "n/a"
                });
                });


                That will create a document for the user in a collection called users, identified by its UID. Beware that the displayName is not necessarily set in the auth user object. So you might want to set a default placeholder that you can query for later and ask the user to provide information for.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 2 at 5:26









                Dennis AlundDennis Alund

                646217




                646217

























                    0














                    var config = {
                    apiKey: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    authDomain: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    databaseURL: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    projectId: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    storageBucket: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    messagingSenderId: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL"
                    };
                    firebase.initializeApp(config);
                    // Initialize Cloud Firestore through Firebase
                    var firestore = firebase.firestore();
                    const settings = {/* your settings... */ timestampsInSnapshots: true};
                    firestore.settings(settings);

                    firebase.auth().onAuthStateChanged(function(user) {
                    //check if user is signed in
                    if (user) {
                    //user is signed in
                    //get user display
                    if (user.displayName !== undefined) {
                    var username = user.displayName;
                    //add displayName to firestore
                    firestore
                    .collection("users")
                    .doc(user.uid)
                    .set({
                    username: username
                    }, {merge: true})
                    .then(function() {
                    alert('displayName added!');
                    })
                    .catch(function(e) {
                    //there was an error writing to firestore
                    //check console
                    console.log(e);
                    });
                    } else {
                    //user has not set displayName
                    alert('no displayName');
                    }
                    }
                    });





                    share|improve this answer
























                    • Appreciate your contribution and welcome stakoverflow, but I'm asking about firestore not firebase

                      – Hasan A Yousef
                      Jan 2 at 17:26
















                    0














                    var config = {
                    apiKey: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    authDomain: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    databaseURL: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    projectId: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    storageBucket: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    messagingSenderId: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL"
                    };
                    firebase.initializeApp(config);
                    // Initialize Cloud Firestore through Firebase
                    var firestore = firebase.firestore();
                    const settings = {/* your settings... */ timestampsInSnapshots: true};
                    firestore.settings(settings);

                    firebase.auth().onAuthStateChanged(function(user) {
                    //check if user is signed in
                    if (user) {
                    //user is signed in
                    //get user display
                    if (user.displayName !== undefined) {
                    var username = user.displayName;
                    //add displayName to firestore
                    firestore
                    .collection("users")
                    .doc(user.uid)
                    .set({
                    username: username
                    }, {merge: true})
                    .then(function() {
                    alert('displayName added!');
                    })
                    .catch(function(e) {
                    //there was an error writing to firestore
                    //check console
                    console.log(e);
                    });
                    } else {
                    //user has not set displayName
                    alert('no displayName');
                    }
                    }
                    });





                    share|improve this answer
























                    • Appreciate your contribution and welcome stakoverflow, but I'm asking about firestore not firebase

                      – Hasan A Yousef
                      Jan 2 at 17:26














                    0












                    0








                    0







                    var config = {
                    apiKey: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    authDomain: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    databaseURL: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    projectId: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    storageBucket: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    messagingSenderId: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL"
                    };
                    firebase.initializeApp(config);
                    // Initialize Cloud Firestore through Firebase
                    var firestore = firebase.firestore();
                    const settings = {/* your settings... */ timestampsInSnapshots: true};
                    firestore.settings(settings);

                    firebase.auth().onAuthStateChanged(function(user) {
                    //check if user is signed in
                    if (user) {
                    //user is signed in
                    //get user display
                    if (user.displayName !== undefined) {
                    var username = user.displayName;
                    //add displayName to firestore
                    firestore
                    .collection("users")
                    .doc(user.uid)
                    .set({
                    username: username
                    }, {merge: true})
                    .then(function() {
                    alert('displayName added!');
                    })
                    .catch(function(e) {
                    //there was an error writing to firestore
                    //check console
                    console.log(e);
                    });
                    } else {
                    //user has not set displayName
                    alert('no displayName');
                    }
                    }
                    });





                    share|improve this answer













                    var config = {
                    apiKey: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    authDomain: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    databaseURL: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    projectId: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    storageBucket: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL",
                    messagingSenderId: "REPLACE_WITH_YOUR_FIREBASE_PROJECT_CREDENTIAL"
                    };
                    firebase.initializeApp(config);
                    // Initialize Cloud Firestore through Firebase
                    var firestore = firebase.firestore();
                    const settings = {/* your settings... */ timestampsInSnapshots: true};
                    firestore.settings(settings);

                    firebase.auth().onAuthStateChanged(function(user) {
                    //check if user is signed in
                    if (user) {
                    //user is signed in
                    //get user display
                    if (user.displayName !== undefined) {
                    var username = user.displayName;
                    //add displayName to firestore
                    firestore
                    .collection("users")
                    .doc(user.uid)
                    .set({
                    username: username
                    }, {merge: true})
                    .then(function() {
                    alert('displayName added!');
                    })
                    .catch(function(e) {
                    //there was an error writing to firestore
                    //check console
                    console.log(e);
                    });
                    } else {
                    //user has not set displayName
                    alert('no displayName');
                    }
                    }
                    });






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 2 at 13:19









                    Mwila KaundaMwila Kaunda

                    11




                    11













                    • Appreciate your contribution and welcome stakoverflow, but I'm asking about firestore not firebase

                      – Hasan A Yousef
                      Jan 2 at 17:26



















                    • Appreciate your contribution and welcome stakoverflow, but I'm asking about firestore not firebase

                      – Hasan A Yousef
                      Jan 2 at 17:26

















                    Appreciate your contribution and welcome stakoverflow, but I'm asking about firestore not firebase

                    – Hasan A Yousef
                    Jan 2 at 17:26





                    Appreciate your contribution and welcome stakoverflow, but I'm asking about firestore not firebase

                    – Hasan A Yousef
                    Jan 2 at 17:26


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001429%2fadd-user-displayname%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas