Firebase authWithOAuthRedirect() woes

别等时光非礼了梦想. 提交于 2019-11-28 07:36:40

问题


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 Firebase core.

I have been able to successfully implement authentication using

authWithOAuthPopup("<provider>", function(error, authData) { ... });

It accepts a callback, which is passed authentication data in authData.

On the contrary, I can't undersand how to use

authWithOAuthRedirect("<provider>", function(error) { ... });

Firebase Authentication docs page is very concise... :-(. This is all what is said:

Alternatively [instead of authWithOAuthPopup], you may prompt the user to login with a full browser redirect, and Firebase will automatically restore the session when you return to the originating page

How do I get authData, when Firebase - after redirection - returns to my page?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/26390027/firebase-authwithoauthredirect-woes

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