Firebase authWithOAuthRedirect() woes

半城伤御伤魂 提交于 2019-11-29 14:05:46

The authData is available by registering a listener directly on the ref (so before calling authWithOAuthRedirect).

ref.onAuth(function(authData) { 
    ... 
} 
ref.authWithOAuthRedirect("google", function(error) { ... });

See https://www.firebase.com/docs/web/guide/user-auth.html#section-monitoring-authentication

I think I'm running into the same issue as you. I'm trying to do Facebook authentication.

First, I'd like to clarify the reproduction steps for my issue.

  1. My app is loaded on the client.
  2. User clicks login with Facebook.
  3. ref.authWithOAuthRedirect('facebook', ...) is called.
  4. Client is redirected to Facebook and Facebook redirects client back to Firebase app
  5. Despite successful authentication with Facebook, the callback passed to onAuth() is invoked (only once) with authData === null.

The callback passed to onAuth() is not invoked a second time with correct authData.

However, reloading the app causes the callback passed to onAuth to be invoked with correct authData. The reasons for this are not known to me but I suspect race condition.

Here's my workaround.

Before calling ref.authWithOAuthRedirect('facebook', ...) set yourself a flag in sessionStorage.

sessionStorage.reload = true;
ref.authWithOAuthRedirect('facebook', ...)

When the client is redirected to your app back from Facebook, you should be able to check for this flag and reload the page if necessary.

if (sessionStorage.reload) {
    delete sessionStorage.reload;
    setTimeout(function() {
        location.reload();
    }, 1000)
}

setTimeout(function() { ... }, 1000) helps fight the assumed race condition. I found 500 ms is insufficient time for the race condition to be resolved.

And one small gotcha: if you reload the page too soon, then authData remains null no matter how many times you reload the page.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!