How to create user and upload data on Firebase in one single transaction












2















I am making a web application which creates a user on Firebase using email and password firebase.auth().createUserUsingEmailAndPassowrd()



After the account is created, I immediately upload some data based on the forms values on the firebase database.



All works well, but sometimes, In worst case scenarios, the user is created but due to network issues the data is not uploaded. And the app is already redirected to a place where it needs that data and the whole app fails.



How can I make it certain that the user account is created only if the data I am trying to upload on database gets updated too.



Summary : Make sure that some data is uploaded along with user creation or the user doesn't get created at all.










share|improve this question



























    2















    I am making a web application which creates a user on Firebase using email and password firebase.auth().createUserUsingEmailAndPassowrd()



    After the account is created, I immediately upload some data based on the forms values on the firebase database.



    All works well, but sometimes, In worst case scenarios, the user is created but due to network issues the data is not uploaded. And the app is already redirected to a place where it needs that data and the whole app fails.



    How can I make it certain that the user account is created only if the data I am trying to upload on database gets updated too.



    Summary : Make sure that some data is uploaded along with user creation or the user doesn't get created at all.










    share|improve this question

























      2












      2








      2








      I am making a web application which creates a user on Firebase using email and password firebase.auth().createUserUsingEmailAndPassowrd()



      After the account is created, I immediately upload some data based on the forms values on the firebase database.



      All works well, but sometimes, In worst case scenarios, the user is created but due to network issues the data is not uploaded. And the app is already redirected to a place where it needs that data and the whole app fails.



      How can I make it certain that the user account is created only if the data I am trying to upload on database gets updated too.



      Summary : Make sure that some data is uploaded along with user creation or the user doesn't get created at all.










      share|improve this question














      I am making a web application which creates a user on Firebase using email and password firebase.auth().createUserUsingEmailAndPassowrd()



      After the account is created, I immediately upload some data based on the forms values on the firebase database.



      All works well, but sometimes, In worst case scenarios, the user is created but due to network issues the data is not uploaded. And the app is already redirected to a place where it needs that data and the whole app fails.



      How can I make it certain that the user account is created only if the data I am trying to upload on database gets updated too.



      Summary : Make sure that some data is uploaded along with user creation or the user doesn't get created at all.







      android firebase react-native firebase-realtime-database firebase-authentication






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 26 '17 at 11:24









      Prakhar SharmaPrakhar Sharma

      5017




      5017
























          4 Answers
          4






          active

          oldest

          votes


















          1














          Because you're so afraid of worst case scenario (which is absolutely normal), here is one way to do it: Consider you have these fields: email, password, and address. The address field is the one you need to store in your database (with user uid, of course).



          First you try to register the user, and like Faizy said, you use onCompleteListener like this



          mAuth.createUserWithEmailAndPassword(email, password)
          .onCompleteListener(... {
          ... onComplete (... task) {
          if (task.isSuccessful()) {
          doInsertTheData();
          }
          ...


          When task is successful, then you want to insert the data into database, which doInsertTheData() is created for



          private void doInsertTheData() {
          // I believe you familiar with Firebase database inserting method, so I skip the long part
          FirebaseDatabase...setValue(address, new CompletionListener() {
          ... onComplete(FirebaseError firebaseError, ...) {
          if (firebaseError == null) {
          // if your code reach here, its a happy ending
          } else {
          // if it reach here, then you can remove the signed in user
          // and tell the user that their registration is failed and should try again
          FirebaseAuth.getInstance().getCurrentUser().delete();
          }


          And there you go, as long as the database saving process failed, the user credential will be deleted




          Note: I actually have another method in mind when I type this, but it's too complicated and I think this one is more easy to understand (I haven't try/use this though)







          share|improve this answer





















          • 1





            Good work! This is what I'd call a more resilient client. But you'd still have inconsistency if the client app crashes between the completion of the creation of the user and it writing to the database. To further reduce chances of that, I'd move this code into Cloud Functions trigger of the user creation: firebase.google.com/docs/functions/auth-events

            – Frank van Puffelen
            May 18 '17 at 23:28











          • @FrankvanPuffelen How does the cloud function trigger have access to the data needed to populate user record? ("address, ...")

            – cdock
            May 23 '17 at 20:51



















          3














          firebaser here



          There are no cross-feature transactions in Firebase at the moment. The common approach is to:




          • reduce the risk of cross-feature problems by carefully ordering your code

          • increase the resilience of your code against cross-feature failures


          This means you decide which one if more important: that each existing user has a database node or that each database node points to an existing user. You seem to want to answer "both", but as said: that's not possible, you'll have to pick. In most scenarios I've seen developers will pick option 1: every Firebase Authentication user must have a node in the database.



          In that case, you'll want to run the code that registers a user in the database before actually registering the user with Firebase Authentication. And since the user only gets their UID once they do register with Auth, you'll need some two-stepped process here. All quite nasty code that is bound to have problems. Which is why you'll see that most developers with an app in production care a lot less about this approach than you think.



          Instead they rely on regular clean-up of their database. They run an administrative process every day or so that scans the database for users without a corresponding Firebase Authentication registration. This handles all kinds of edge cases with a fairly simple process, even edge cases not related to registration. You might even call this "eventual consistency": eventually the data in the database will match the registered users.



          Final shout-out on your original question: you can drastically reduce the chance of a cross-feature failure by running the register-and-write-to-database code in a Cloud Function for Firebase. Using the Admin SDK in a Cloud Function makes this all nice and tidy. But while the chance of failures reduces a lot, it can still fail. So if you care about the cleanliness of your data, you'll still need the clean-up process.






          share|improve this answer
























          • Does anybody know if cross-feature transactions are going to be supported in the future?

            – Yulian
            Aug 9 '18 at 13:31











          • While it's hard to say what will happen in the future, nobody is actively working on this at the moment.

            – Frank van Puffelen
            Aug 9 '18 at 14:39











          • Okay, thanks for helping me out.

            – Yulian
            Aug 9 '18 at 19:20



















          1














          You can do it in your OnComplete method of createUserUsingEmailAndPassowrd().



          mAuth.createUserWithEmailAndPassword(email, password)
          .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
          @Override
          public void onComplete(@NonNull Task<AuthResult> task) {

          if (task.isSuccessful()) {
          //Create your user in DB here

          }
          }
          });





          share|improve this answer
























          • is it possible that the user gets created and immediately i loose the connection (extreme paranoid) and the onComplete method fails. So i am left with the created user only. and not the data inside the db.

            – Prakhar Sharma
            Mar 26 '17 at 12:05



















          0














          If you need user to have certain data to use app, then might have to always check.. when they sign in:



          firebase.auth().onAuthStateChanged(user => {
          if (user) {
          // ... access ref to userData field(s) you care about..
          someUserFieldRef.once('value').then(snap => {
          if (! snap.exists()) {
          // redirect to setup page to add missing data..
          }
          })

          }
          })


          If they can use other parts of app, then you can defer check to when specific user data is necessary, get currentUser, then check for data..






          share|improve this answer























            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%2f43028090%2fhow-to-create-user-and-upload-data-on-firebase-in-one-single-transaction%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Because you're so afraid of worst case scenario (which is absolutely normal), here is one way to do it: Consider you have these fields: email, password, and address. The address field is the one you need to store in your database (with user uid, of course).



            First you try to register the user, and like Faizy said, you use onCompleteListener like this



            mAuth.createUserWithEmailAndPassword(email, password)
            .onCompleteListener(... {
            ... onComplete (... task) {
            if (task.isSuccessful()) {
            doInsertTheData();
            }
            ...


            When task is successful, then you want to insert the data into database, which doInsertTheData() is created for



            private void doInsertTheData() {
            // I believe you familiar with Firebase database inserting method, so I skip the long part
            FirebaseDatabase...setValue(address, new CompletionListener() {
            ... onComplete(FirebaseError firebaseError, ...) {
            if (firebaseError == null) {
            // if your code reach here, its a happy ending
            } else {
            // if it reach here, then you can remove the signed in user
            // and tell the user that their registration is failed and should try again
            FirebaseAuth.getInstance().getCurrentUser().delete();
            }


            And there you go, as long as the database saving process failed, the user credential will be deleted




            Note: I actually have another method in mind when I type this, but it's too complicated and I think this one is more easy to understand (I haven't try/use this though)







            share|improve this answer





















            • 1





              Good work! This is what I'd call a more resilient client. But you'd still have inconsistency if the client app crashes between the completion of the creation of the user and it writing to the database. To further reduce chances of that, I'd move this code into Cloud Functions trigger of the user creation: firebase.google.com/docs/functions/auth-events

              – Frank van Puffelen
              May 18 '17 at 23:28











            • @FrankvanPuffelen How does the cloud function trigger have access to the data needed to populate user record? ("address, ...")

              – cdock
              May 23 '17 at 20:51
















            1














            Because you're so afraid of worst case scenario (which is absolutely normal), here is one way to do it: Consider you have these fields: email, password, and address. The address field is the one you need to store in your database (with user uid, of course).



            First you try to register the user, and like Faizy said, you use onCompleteListener like this



            mAuth.createUserWithEmailAndPassword(email, password)
            .onCompleteListener(... {
            ... onComplete (... task) {
            if (task.isSuccessful()) {
            doInsertTheData();
            }
            ...


            When task is successful, then you want to insert the data into database, which doInsertTheData() is created for



            private void doInsertTheData() {
            // I believe you familiar with Firebase database inserting method, so I skip the long part
            FirebaseDatabase...setValue(address, new CompletionListener() {
            ... onComplete(FirebaseError firebaseError, ...) {
            if (firebaseError == null) {
            // if your code reach here, its a happy ending
            } else {
            // if it reach here, then you can remove the signed in user
            // and tell the user that their registration is failed and should try again
            FirebaseAuth.getInstance().getCurrentUser().delete();
            }


            And there you go, as long as the database saving process failed, the user credential will be deleted




            Note: I actually have another method in mind when I type this, but it's too complicated and I think this one is more easy to understand (I haven't try/use this though)







            share|improve this answer





















            • 1





              Good work! This is what I'd call a more resilient client. But you'd still have inconsistency if the client app crashes between the completion of the creation of the user and it writing to the database. To further reduce chances of that, I'd move this code into Cloud Functions trigger of the user creation: firebase.google.com/docs/functions/auth-events

              – Frank van Puffelen
              May 18 '17 at 23:28











            • @FrankvanPuffelen How does the cloud function trigger have access to the data needed to populate user record? ("address, ...")

              – cdock
              May 23 '17 at 20:51














            1












            1








            1







            Because you're so afraid of worst case scenario (which is absolutely normal), here is one way to do it: Consider you have these fields: email, password, and address. The address field is the one you need to store in your database (with user uid, of course).



            First you try to register the user, and like Faizy said, you use onCompleteListener like this



            mAuth.createUserWithEmailAndPassword(email, password)
            .onCompleteListener(... {
            ... onComplete (... task) {
            if (task.isSuccessful()) {
            doInsertTheData();
            }
            ...


            When task is successful, then you want to insert the data into database, which doInsertTheData() is created for



            private void doInsertTheData() {
            // I believe you familiar with Firebase database inserting method, so I skip the long part
            FirebaseDatabase...setValue(address, new CompletionListener() {
            ... onComplete(FirebaseError firebaseError, ...) {
            if (firebaseError == null) {
            // if your code reach here, its a happy ending
            } else {
            // if it reach here, then you can remove the signed in user
            // and tell the user that their registration is failed and should try again
            FirebaseAuth.getInstance().getCurrentUser().delete();
            }


            And there you go, as long as the database saving process failed, the user credential will be deleted




            Note: I actually have another method in mind when I type this, but it's too complicated and I think this one is more easy to understand (I haven't try/use this though)







            share|improve this answer















            Because you're so afraid of worst case scenario (which is absolutely normal), here is one way to do it: Consider you have these fields: email, password, and address. The address field is the one you need to store in your database (with user uid, of course).



            First you try to register the user, and like Faizy said, you use onCompleteListener like this



            mAuth.createUserWithEmailAndPassword(email, password)
            .onCompleteListener(... {
            ... onComplete (... task) {
            if (task.isSuccessful()) {
            doInsertTheData();
            }
            ...


            When task is successful, then you want to insert the data into database, which doInsertTheData() is created for



            private void doInsertTheData() {
            // I believe you familiar with Firebase database inserting method, so I skip the long part
            FirebaseDatabase...setValue(address, new CompletionListener() {
            ... onComplete(FirebaseError firebaseError, ...) {
            if (firebaseError == null) {
            // if your code reach here, its a happy ending
            } else {
            // if it reach here, then you can remove the signed in user
            // and tell the user that their registration is failed and should try again
            FirebaseAuth.getInstance().getCurrentUser().delete();
            }


            And there you go, as long as the database saving process failed, the user credential will be deleted




            Note: I actually have another method in mind when I type this, but it's too complicated and I think this one is more easy to understand (I haven't try/use this though)








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 23 '17 at 11:54









            Community

            11




            11










            answered Mar 26 '17 at 14:21









            koceengkoceeng

            1,88321133




            1,88321133








            • 1





              Good work! This is what I'd call a more resilient client. But you'd still have inconsistency if the client app crashes between the completion of the creation of the user and it writing to the database. To further reduce chances of that, I'd move this code into Cloud Functions trigger of the user creation: firebase.google.com/docs/functions/auth-events

              – Frank van Puffelen
              May 18 '17 at 23:28











            • @FrankvanPuffelen How does the cloud function trigger have access to the data needed to populate user record? ("address, ...")

              – cdock
              May 23 '17 at 20:51














            • 1





              Good work! This is what I'd call a more resilient client. But you'd still have inconsistency if the client app crashes between the completion of the creation of the user and it writing to the database. To further reduce chances of that, I'd move this code into Cloud Functions trigger of the user creation: firebase.google.com/docs/functions/auth-events

              – Frank van Puffelen
              May 18 '17 at 23:28











            • @FrankvanPuffelen How does the cloud function trigger have access to the data needed to populate user record? ("address, ...")

              – cdock
              May 23 '17 at 20:51








            1




            1





            Good work! This is what I'd call a more resilient client. But you'd still have inconsistency if the client app crashes between the completion of the creation of the user and it writing to the database. To further reduce chances of that, I'd move this code into Cloud Functions trigger of the user creation: firebase.google.com/docs/functions/auth-events

            – Frank van Puffelen
            May 18 '17 at 23:28





            Good work! This is what I'd call a more resilient client. But you'd still have inconsistency if the client app crashes between the completion of the creation of the user and it writing to the database. To further reduce chances of that, I'd move this code into Cloud Functions trigger of the user creation: firebase.google.com/docs/functions/auth-events

            – Frank van Puffelen
            May 18 '17 at 23:28













            @FrankvanPuffelen How does the cloud function trigger have access to the data needed to populate user record? ("address, ...")

            – cdock
            May 23 '17 at 20:51





            @FrankvanPuffelen How does the cloud function trigger have access to the data needed to populate user record? ("address, ...")

            – cdock
            May 23 '17 at 20:51













            3














            firebaser here



            There are no cross-feature transactions in Firebase at the moment. The common approach is to:




            • reduce the risk of cross-feature problems by carefully ordering your code

            • increase the resilience of your code against cross-feature failures


            This means you decide which one if more important: that each existing user has a database node or that each database node points to an existing user. You seem to want to answer "both", but as said: that's not possible, you'll have to pick. In most scenarios I've seen developers will pick option 1: every Firebase Authentication user must have a node in the database.



            In that case, you'll want to run the code that registers a user in the database before actually registering the user with Firebase Authentication. And since the user only gets their UID once they do register with Auth, you'll need some two-stepped process here. All quite nasty code that is bound to have problems. Which is why you'll see that most developers with an app in production care a lot less about this approach than you think.



            Instead they rely on regular clean-up of their database. They run an administrative process every day or so that scans the database for users without a corresponding Firebase Authentication registration. This handles all kinds of edge cases with a fairly simple process, even edge cases not related to registration. You might even call this "eventual consistency": eventually the data in the database will match the registered users.



            Final shout-out on your original question: you can drastically reduce the chance of a cross-feature failure by running the register-and-write-to-database code in a Cloud Function for Firebase. Using the Admin SDK in a Cloud Function makes this all nice and tidy. But while the chance of failures reduces a lot, it can still fail. So if you care about the cleanliness of your data, you'll still need the clean-up process.






            share|improve this answer
























            • Does anybody know if cross-feature transactions are going to be supported in the future?

              – Yulian
              Aug 9 '18 at 13:31











            • While it's hard to say what will happen in the future, nobody is actively working on this at the moment.

              – Frank van Puffelen
              Aug 9 '18 at 14:39











            • Okay, thanks for helping me out.

              – Yulian
              Aug 9 '18 at 19:20
















            3














            firebaser here



            There are no cross-feature transactions in Firebase at the moment. The common approach is to:




            • reduce the risk of cross-feature problems by carefully ordering your code

            • increase the resilience of your code against cross-feature failures


            This means you decide which one if more important: that each existing user has a database node or that each database node points to an existing user. You seem to want to answer "both", but as said: that's not possible, you'll have to pick. In most scenarios I've seen developers will pick option 1: every Firebase Authentication user must have a node in the database.



            In that case, you'll want to run the code that registers a user in the database before actually registering the user with Firebase Authentication. And since the user only gets their UID once they do register with Auth, you'll need some two-stepped process here. All quite nasty code that is bound to have problems. Which is why you'll see that most developers with an app in production care a lot less about this approach than you think.



            Instead they rely on regular clean-up of their database. They run an administrative process every day or so that scans the database for users without a corresponding Firebase Authentication registration. This handles all kinds of edge cases with a fairly simple process, even edge cases not related to registration. You might even call this "eventual consistency": eventually the data in the database will match the registered users.



            Final shout-out on your original question: you can drastically reduce the chance of a cross-feature failure by running the register-and-write-to-database code in a Cloud Function for Firebase. Using the Admin SDK in a Cloud Function makes this all nice and tidy. But while the chance of failures reduces a lot, it can still fail. So if you care about the cleanliness of your data, you'll still need the clean-up process.






            share|improve this answer
























            • Does anybody know if cross-feature transactions are going to be supported in the future?

              – Yulian
              Aug 9 '18 at 13:31











            • While it's hard to say what will happen in the future, nobody is actively working on this at the moment.

              – Frank van Puffelen
              Aug 9 '18 at 14:39











            • Okay, thanks for helping me out.

              – Yulian
              Aug 9 '18 at 19:20














            3












            3








            3







            firebaser here



            There are no cross-feature transactions in Firebase at the moment. The common approach is to:




            • reduce the risk of cross-feature problems by carefully ordering your code

            • increase the resilience of your code against cross-feature failures


            This means you decide which one if more important: that each existing user has a database node or that each database node points to an existing user. You seem to want to answer "both", but as said: that's not possible, you'll have to pick. In most scenarios I've seen developers will pick option 1: every Firebase Authentication user must have a node in the database.



            In that case, you'll want to run the code that registers a user in the database before actually registering the user with Firebase Authentication. And since the user only gets their UID once they do register with Auth, you'll need some two-stepped process here. All quite nasty code that is bound to have problems. Which is why you'll see that most developers with an app in production care a lot less about this approach than you think.



            Instead they rely on regular clean-up of their database. They run an administrative process every day or so that scans the database for users without a corresponding Firebase Authentication registration. This handles all kinds of edge cases with a fairly simple process, even edge cases not related to registration. You might even call this "eventual consistency": eventually the data in the database will match the registered users.



            Final shout-out on your original question: you can drastically reduce the chance of a cross-feature failure by running the register-and-write-to-database code in a Cloud Function for Firebase. Using the Admin SDK in a Cloud Function makes this all nice and tidy. But while the chance of failures reduces a lot, it can still fail. So if you care about the cleanliness of your data, you'll still need the clean-up process.






            share|improve this answer













            firebaser here



            There are no cross-feature transactions in Firebase at the moment. The common approach is to:




            • reduce the risk of cross-feature problems by carefully ordering your code

            • increase the resilience of your code against cross-feature failures


            This means you decide which one if more important: that each existing user has a database node or that each database node points to an existing user. You seem to want to answer "both", but as said: that's not possible, you'll have to pick. In most scenarios I've seen developers will pick option 1: every Firebase Authentication user must have a node in the database.



            In that case, you'll want to run the code that registers a user in the database before actually registering the user with Firebase Authentication. And since the user only gets their UID once they do register with Auth, you'll need some two-stepped process here. All quite nasty code that is bound to have problems. Which is why you'll see that most developers with an app in production care a lot less about this approach than you think.



            Instead they rely on regular clean-up of their database. They run an administrative process every day or so that scans the database for users without a corresponding Firebase Authentication registration. This handles all kinds of edge cases with a fairly simple process, even edge cases not related to registration. You might even call this "eventual consistency": eventually the data in the database will match the registered users.



            Final shout-out on your original question: you can drastically reduce the chance of a cross-feature failure by running the register-and-write-to-database code in a Cloud Function for Firebase. Using the Admin SDK in a Cloud Function makes this all nice and tidy. But while the chance of failures reduces a lot, it can still fail. So if you care about the cleanliness of your data, you'll still need the clean-up process.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 26 '17 at 15:45









            Frank van PuffelenFrank van Puffelen

            237k29382408




            237k29382408













            • Does anybody know if cross-feature transactions are going to be supported in the future?

              – Yulian
              Aug 9 '18 at 13:31











            • While it's hard to say what will happen in the future, nobody is actively working on this at the moment.

              – Frank van Puffelen
              Aug 9 '18 at 14:39











            • Okay, thanks for helping me out.

              – Yulian
              Aug 9 '18 at 19:20



















            • Does anybody know if cross-feature transactions are going to be supported in the future?

              – Yulian
              Aug 9 '18 at 13:31











            • While it's hard to say what will happen in the future, nobody is actively working on this at the moment.

              – Frank van Puffelen
              Aug 9 '18 at 14:39











            • Okay, thanks for helping me out.

              – Yulian
              Aug 9 '18 at 19:20

















            Does anybody know if cross-feature transactions are going to be supported in the future?

            – Yulian
            Aug 9 '18 at 13:31





            Does anybody know if cross-feature transactions are going to be supported in the future?

            – Yulian
            Aug 9 '18 at 13:31













            While it's hard to say what will happen in the future, nobody is actively working on this at the moment.

            – Frank van Puffelen
            Aug 9 '18 at 14:39





            While it's hard to say what will happen in the future, nobody is actively working on this at the moment.

            – Frank van Puffelen
            Aug 9 '18 at 14:39













            Okay, thanks for helping me out.

            – Yulian
            Aug 9 '18 at 19:20





            Okay, thanks for helping me out.

            – Yulian
            Aug 9 '18 at 19:20











            1














            You can do it in your OnComplete method of createUserUsingEmailAndPassowrd().



            mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

            if (task.isSuccessful()) {
            //Create your user in DB here

            }
            }
            });





            share|improve this answer
























            • is it possible that the user gets created and immediately i loose the connection (extreme paranoid) and the onComplete method fails. So i am left with the created user only. and not the data inside the db.

              – Prakhar Sharma
              Mar 26 '17 at 12:05
















            1














            You can do it in your OnComplete method of createUserUsingEmailAndPassowrd().



            mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

            if (task.isSuccessful()) {
            //Create your user in DB here

            }
            }
            });





            share|improve this answer
























            • is it possible that the user gets created and immediately i loose the connection (extreme paranoid) and the onComplete method fails. So i am left with the created user only. and not the data inside the db.

              – Prakhar Sharma
              Mar 26 '17 at 12:05














            1












            1








            1







            You can do it in your OnComplete method of createUserUsingEmailAndPassowrd().



            mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

            if (task.isSuccessful()) {
            //Create your user in DB here

            }
            }
            });





            share|improve this answer













            You can do it in your OnComplete method of createUserUsingEmailAndPassowrd().



            mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

            if (task.isSuccessful()) {
            //Create your user in DB here

            }
            }
            });






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 26 '17 at 11:51









            Muhammad Farrukh FaizyMuhammad Farrukh Faizy

            5721819




            5721819













            • is it possible that the user gets created and immediately i loose the connection (extreme paranoid) and the onComplete method fails. So i am left with the created user only. and not the data inside the db.

              – Prakhar Sharma
              Mar 26 '17 at 12:05



















            • is it possible that the user gets created and immediately i loose the connection (extreme paranoid) and the onComplete method fails. So i am left with the created user only. and not the data inside the db.

              – Prakhar Sharma
              Mar 26 '17 at 12:05

















            is it possible that the user gets created and immediately i loose the connection (extreme paranoid) and the onComplete method fails. So i am left with the created user only. and not the data inside the db.

            – Prakhar Sharma
            Mar 26 '17 at 12:05





            is it possible that the user gets created and immediately i loose the connection (extreme paranoid) and the onComplete method fails. So i am left with the created user only. and not the data inside the db.

            – Prakhar Sharma
            Mar 26 '17 at 12:05











            0














            If you need user to have certain data to use app, then might have to always check.. when they sign in:



            firebase.auth().onAuthStateChanged(user => {
            if (user) {
            // ... access ref to userData field(s) you care about..
            someUserFieldRef.once('value').then(snap => {
            if (! snap.exists()) {
            // redirect to setup page to add missing data..
            }
            })

            }
            })


            If they can use other parts of app, then you can defer check to when specific user data is necessary, get currentUser, then check for data..






            share|improve this answer




























              0














              If you need user to have certain data to use app, then might have to always check.. when they sign in:



              firebase.auth().onAuthStateChanged(user => {
              if (user) {
              // ... access ref to userData field(s) you care about..
              someUserFieldRef.once('value').then(snap => {
              if (! snap.exists()) {
              // redirect to setup page to add missing data..
              }
              })

              }
              })


              If they can use other parts of app, then you can defer check to when specific user data is necessary, get currentUser, then check for data..






              share|improve this answer


























                0












                0








                0







                If you need user to have certain data to use app, then might have to always check.. when they sign in:



                firebase.auth().onAuthStateChanged(user => {
                if (user) {
                // ... access ref to userData field(s) you care about..
                someUserFieldRef.once('value').then(snap => {
                if (! snap.exists()) {
                // redirect to setup page to add missing data..
                }
                })

                }
                })


                If they can use other parts of app, then you can defer check to when specific user data is necessary, get currentUser, then check for data..






                share|improve this answer













                If you need user to have certain data to use app, then might have to always check.. when they sign in:



                firebase.auth().onAuthStateChanged(user => {
                if (user) {
                // ... access ref to userData field(s) you care about..
                someUserFieldRef.once('value').then(snap => {
                if (! snap.exists()) {
                // redirect to setup page to add missing data..
                }
                })

                }
                })


                If they can use other parts of app, then you can defer check to when specific user data is necessary, get currentUser, then check for data..







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 23 '17 at 21:08









                cdockcdock

                480710




                480710






























                    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%2f43028090%2fhow-to-create-user-and-upload-data-on-firebase-in-one-single-transaction%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







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini