question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it safe for the merge conflict process of anonymous upgrade to be handled client side?

See original GitHub issue

Is it safe for the merge conflict process of anonymous upgrade to be handled client side? I already have the code working, however, the part where you copy the old data from anonymous user and update it to the new user requires writing to database, is it safe to have client handle the code?

I tried to move the code to Cloud Functions and have Cloud Functions take care of the database writing to Cloud Firestore, but it does not seems to be possible to move all of the code into one Cloud Functions because return firebase.auth().signInWithCredential(cred); needs to be handled by the client and saving anonymous user’s credential on local variable to pass to the Cloud Functions sounds dangerous because user can just manipulate the credential and send the wrong credential to Cloud Functions.

What’s the best way to do this?

The entire workflow of handling merge conflict of anonymous user upgrading that I am talking about is as followed:

// signInFailure callback must be provided to handle merge conflicts which
    // occur when an existing credential is linked to an anonymous user.
    signInFailure: function(error) {
      // For merge conflicts, the error.code will be
      // 'firebaseui/anonymous-upgrade-merge-conflict'.
      if (error.code != 'firebaseui/anonymous-upgrade-merge-conflict') {
        return Promise.resolve();
      }
      // The credential the user tried to sign in with.
      var cred = error.credential;
      // If using Firebase Realtime Database. The anonymous user data has to be
      // copied to the non-anonymous user.
      var app = firebase.app();
      // Save anonymous user data first.
      return app.database().ref('users/' + firebase.auth().currentUser.uid)
          .once('value')
          .then(function(snapshot) {
            data = snapshot.val();
            // This will trigger onAuthStateChanged listener which
            // could trigger a redirect to another page.
            // Ensure the upgrade flow is not interrupted by that callback
            // and that this is given enough time to complete before
            // redirection.
            return firebase.auth().signInWithCredential(cred);
          })
          .then(function(user) {
            // Original Anonymous Auth instance now has the new user.
            return app.database().ref('users/' + user.uid).set(data);
          })
          .then(function() {
            // Delete anonymnous user.
            return anonymousUser.delete();
          }).then(function() {
            // Clear data in case a new user signs in, and the state change
            // triggers.
            data = null;
            // FirebaseUI will reset and the UI cleared when this promise
            // resolves.
            // signInSuccessWithAuthResult will not run. Successful sign-in
            // logic has to be run explicitly.
            window.location.assign('<url-to-redirect-to-on-success>');
          });

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
bojeil-googlecommented, Jun 25, 2020

Hey @christopherread, you can’t call auth.currentUser.linkWithCredential(credential) as it will fail. We try to do that first on your behalf but it will fail if the credential belongs to an existing user. This is what we call a merge conflict and you have to manually copy the data from one use to the other. You could sign in directly with the credential in the error but you would lose the current anonymous auth user.

0reactions
christopherreadcommented, Jun 25, 2020

Thank you, I should have checked the firebaseui source first, before asking!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Resolve Merge Conflicts in Git? | Simplilearn [Updated]
The failure during the merge process indicates that there is a conflict between the local branch and the branch being merged. In this...
Read more >
How to Resolve GitHub Merge Conflicts | Cloudbees Blog
While you may need to resolve merge conflicts more frequently, this means that you'll be resolving smaller conflicts each time.
Read more >
Conflict Detection and Sync - AWS AppSync
Automerge is designed to automatically detect, merge, and retry requests with an updated version, absolving the client from needing to manually merge any ......
Read more >
Rule 1.7 Conflict of Interest: Current Clients - Comment
For former client conflicts of interest, see Rule 1.9. ... lawyer must seek court approval where necessary and take steps to minimize harm...
Read more >
Handling common JavaScript problems - MDN Web Docs
See good-for-loop.html (see the source code also) for a version that ... At this point, the right-hand side will update to show some...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found