firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

Firebase Auth w/Provider: Calling getRedirectResult() always returns NULL user

Open rmcsharry opened this issue 5 years ago • 21 comments

[REQUIRED] Describe your environment

  • Operating System version: MacOS 10.15.3___
  • Browser version: __Chrome Version 81.0.4044.138 ___
  • Firebase SDK version: 7.13.1
  • Firebase Product: _auth (auth, database, storage, etc)

[REQUIRED] Describe the problem

Steps to reproduce:

After calling signInWithRedirect() and then getRedirectResults() on the page load after the login succeeds, the user is always null.

This has been reported before here but was closed.

Relevant Code:

This code runs on page load, but the user is null:

  firebase.auth().getRedirectResult().then(credential => {
    if (credential.user)
      firebase
        .database()
        .ref('users')
        .child(credential.user.uid)
        .child('email')
        .set('[email protected]');
  });

However, in onAuthStateChanged there is a user.

https://stackoverflow.com/questions/62032646/firebase-realtime-database-rules-to-restrict-on-user-id-runs-in-simulator-but-no/62040506#62040506

rmcsharry avatar May 27 '20 10:05 rmcsharry

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar May 27 '20 10:05 google-oss-bot

Thanks for filing this issue! We'll look into this -- filed b/158323213 for internal tracking.

rosalyntan avatar Jun 05 '20 20:06 rosalyntan

Any updates on this issue? I am having the same problem. In a chrome browser in android the user is always null after the redirect. For some other browsers it is working just fine

NickNish09 avatar Jan 28 '21 18:01 NickNish09

@NickNish09 is it just null after getRedirectResult() resolves, or is the user also null in the change listeners (onAuthStateChanged())?

sam-gc avatar Jan 29 '21 00:01 sam-gc

@samhorlbeck it happens on the onAuthStateChanged too. I tried to put it on the page, but the user is alway null:

firebase.auth().onAuthStateChanged(user => {
      if (user) {
        // never gets called
      }
    });

The wierd thing is that it only happens when I am using the firebase js on an Android App that is running a PWA. There are other places that the same code works

NickNish09 avatar Jan 29 '21 01:01 NickNish09

I think I have a similar issue (if not the same). Having the problems on Android App PWA/TWA and Safari on iOS. Chrome, Firefox on iOS is working. And on desktop everything is working (also Safari).

@NickNish09 Do your callback functions get called? I have tried with the following code. And it looks like the Promises do not get resolved, when the problems occur. Also I think these problems started since I have implemented ServiceWorkers. Maybe it has something to do with them, because sometimes it is working in iOS Safari. Need to check that again.

   console.log("calling getRedirectResult()");
   firebase
      .auth()
      .getRedirectResult()
      .then((result) => {
         console.log("getRedirectResult then called", result);
      })
      .catch((error) => {
         console.log("getRedirectResult catch called", error);
      })
      .finally(() => {
         console.log("getRedirectResult finally called");
      });

   firebase.auth().onAuthStateChanged(function (user) {
      console.log("firebase.auth().onAuthStateChanged", user);
   });   
   console.log("event handlers registerd.");

Edit: I have noticed that randomly locking/unlocking the screen of my iPhone (while having the page still in foreground in Safari) will cause the getRedirectResult() Promise to resolve ...

d03090 avatar Jan 31 '21 10:01 d03090

@d03090 Actually they are being called:

const getAuthRedirectResult = useCallback(async () => {
    let requestBody;
    let firebaseUser: firebase.User | null = null;
    let data;
    try {
      const { user } = await firebase.auth().getRedirectResult();
      firebaseUser = user;

      if (firebaseUser) {
        // user is always null, never hits here
      } else {
        // when in an android pwa and hitting a production environment, it always goes to this else
      }
    } catch (error) {
      handleSignInError(error, "getRedirectResult API Error", {
        requestBody,
        firebaseUser,
        responseData: data,
      });
    }
}, [redirectUserToHomePage, getAuthParams, utmParams]);

useEffect(() => {
  getAuthRedirectResult();
}, [getAuthRedirectResult]);

The problem is that the user from firebase.auth().getRedirectResult() is always null when using the app on an android apk that is rendering a webview (chrome webview). I have no clue on what is happening, since it works on the same APK but hitting a different server.

I tried to run the server on my computer in production mode. This same code works. But when hitting a server on heroku (or any other server like in google cloud) it doesn't work. I change the host that the PWA is hitting here:

hostName: 'some-ip-on-heroku', // doesn't work.
hostName: 'some-ip-on-my-localhost-in-production-mode', // works.

NickNish09 avatar Jan 31 '21 13:01 NickNish09

I didn't realize this was happening in a webview / on mobile for some of you. I'm wondering if this is related to https://github.com/firebase/firebase-js-sdk/issues/4256

sam-gc avatar Feb 01 '21 17:02 sam-gc

any solution?

takashimamorino avatar Mar 28 '22 13:03 takashimamorino

I'm having this error as well, any update?

ayush-goyal avatar Sep 15 '22 05:09 ayush-goyal

+1 Getting the issue here as well

nelsongoh avatar Oct 21 '22 17:10 nelsongoh

  • 1 Me as well!

Pckool avatar Oct 21 '22 21:10 Pckool

+1 , any updates?

sprmke avatar Oct 24 '22 22:10 sprmke

Plus one, any solutions?

emcfarlane avatar Nov 13 '22 12:11 emcfarlane

We aren't sure of the rootcause of this, but browser privacy settings could cause it. Can you try one of the mitigations in https://firebase.google.com/docs/auth/web/third-party-storage-mitigation and see if it helps? Thanks!

prameshj avatar Nov 14 '22 18:11 prameshj

I had this exact issue, tracked it down to #6827. You can confirm whether it's the same root cause by adding:

window.addEventListener("beforeunload", () => {
  const pendingRedirectKey = Object.keys(window.sessionStorage).find(key => /^firebase:pendingRedirect:/.test(key));
  if (!pendingRedirectKey) {
    console.log("firebase:pendingRedirect: key missing from sessionStorage, getRedirectResult() will return null");
  }
});

Before your signInWithRedirect() call.

WestonThayer avatar Nov 22 '22 22:11 WestonThayer

I reported this issue but I no longer work on the project, so I cannot check if any of these suggestions work. However, it does look to me as if @WestonThayer's suggestion is the most likely solution.

It would be good know if people can check his solution and report back. Thanks!

rmcsharry avatar Dec 05 '22 13:12 rmcsharry

I'm facing this issue with chrome browser on IOS.

mrthiti avatar Dec 21 '22 19:12 mrthiti

Check out this link: https://firebase.google.com/docs/auth/web/redirect-best-practices#update-authdomain

This is a known issue and it results most likely from browser settings blocking third party cookies. When I allowed third party cookies, the .getRedirectResult() method returned the actual user, but when these cookies are blocked the user is null.

times29 avatar Jan 05 '23 20:01 times29

Check out this link: https://firebase.google.com/docs/auth/web/redirect-best-practices#update-authdomain

This is a known issue and it results most likely from browser settings blocking third party cookies. When I allowed third party cookies, the .getRedirectResult() method returned the actual user, but when these cookies are blocked the user is null.

In addition to this, the fix in https://github.com/firebase/firebase-js-sdk/pull/6914 can also help. It is not released yet, but should be part of the next release.

prameshj avatar Jan 09 '23 20:01 prameshj

Same error with emulator as well

servefast-cto avatar May 23 '24 12:05 servefast-cto