Firebase authWithOAuthRedirect() woes

前端 未结 2 1552
再見小時候
再見小時候 2020-12-20 18:01

I\'m trying to update my angularjs app to support Firebase 1.1 (I was stick with Firebase 1.0.x).
It deprecates firebasesimplelogin, including authentication inside Fire

相关标签:
2条回答
  • 2020-12-20 18:42

    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.

    0 讨论(0)
  • 2020-12-20 18:54

    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

    0 讨论(0)
提交回复
热议问题