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:
- Create Firebase UI widget with
signInSuccessWithAuthResult
callback and async code within. - Sign up for a new user account with Google
- 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:
When it successfully writes:
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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: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 thethen
syntax.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 asignInSuccessUrl
, thus not interrupting your success logic. You also have to ensure that no other redirect in your own logic interrupts your own logic.