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.

signInSuccessWithAuthResult callback does not work consistently with async code

See original GitHub issue

[REQUIRED] Describe your environment

  • Operating System version: Windows 10
  • Browser version: Chrome 88.0.4324.146 x64
  • Firebase UI version: 4.7.3
  • Firebase SDK version: 8.2.6

[REQUIRED] Describe the problem

Async code within signInSuccessWithAuthResult does not always finish, or the auth callback isn’t finished yet when the async code starts (not 100% sure which one).

Steps to reproduce:

  1. Create Firebase UI widget with signInSuccessWithAuthResult callback and async code within.
  2. Sign up for a new user account with Google
  3. Watch as async code does not always finish running.

Relevant Code:

callbacks: {
  signInSuccessWithAuthResult: (authResult) => {
    const uid = authResult.user.uid;
    console.log("uid: " + uid);

    // Add a new document to Firestore
    db.collection("users")
      .doc(uid)
      .set({
        createdAt: firebase.firestore.FieldValue.serverTimestamp(),
        processed: false,
      })
      .then(() => {
        console.log("in promise");
        // Redirect to sign up step 2
        this.$router.push({ name: "Sign Up 2" });
      })
      .catch((error) => {
        console.error("Error adding document: ", error);
      });
    console.log("end of auth callback");
  },
},

It looks like the auth result does not finalize before the write to Firestore starts. I get this error sometimes (yes, my rules are set up correctly and sometimes I get the “in promise” line). But notice how it prints the error (or “in promise”) after the “end of auth callback” log.

When it gets the error: image

When it successfully writes: image

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
davidstackiocommented, Feb 8, 2021

@bojeil-google @yingqi-chen Thank you both for helping me get to the bottom of this. It looks like it’s working as expected now. The main problem was not explicitly returning false like this:

...
console.log("end of auth callback");
return false;

As an aside, the async/await syntax did not work (I didn’t even get the “in promise” print out), so I left it with the then syntax.

1reaction
bojeil-googlecommented, Feb 8, 2021

I think one good practice if you want to run your own async logic on success and redirect is to do something similar to what @yingqi-chen described. You just return false. This means firebaseui will run the callback and since it returns false, it will not redirect to a signInSuccessUrl, thus not interrupting your success logic. You also have to ensure that no other redirect in your own logic interrupts your own logic.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Firebase UI signInSuccessWithAuthResult asynchronous
The signInSuccessWithAuthResult callback expects a synchronous function. Change to the following: callbacks: { signInSuccessWithAuthResult: ...
Read more >
Creating a sign-in page for multiple tenants - Google Cloud
This document shows you how to build a tenant-specific sign-in page for Identity Platform using FirebaseUI, a collection of open-source, pre-built UI components ......
Read more >
Calling asynchronous Firebase APIs from Swift - Peter Friese
Callbacks are probably the most commonly used way to implement asynchronous APIs, and you very likely already used them in your code.
Read more >
Authorization enforcement for Cloud Run - Medium
Cloud Run allows us to expose REST based services implemented within Docker containers. When we consider a REST service, we find that it...
Read more >
How to use Firebase UI to control data flow after signing in ...
Another problem I have when I play with signInSuccessWithAuthResult is that since it asks for exactly a boolean value, when I use async/await...
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