【问题标题】:I cannot get authData by using authWithOAuthRedirect with Chrome on IOS我无法通过在 IOS 上将 authWithOAuthRedirect 与 Chrome 一起使用来获取 authData
【发布时间】:2015-02-06 21:42:56
【问题描述】:

我正在将我的项目与 facebook auth login 集成,并且我想在 IOS 上支持 Chrome。我注意到在这种情况下我必须同时处理 authWithOAuthPopup 和 authWithRedirect (firebase user-auth)。但是 Chrome IOS 目前不支持 Popup auth。

我简化了我的代码并展示了它在 IOS 上的 Chrome 中是如何工作的

    var rootRef = new Firebase('https://docs-sandbox.firebaseio.com/web/uauth');
    rootRef.onAuth( function(authData){
        alert('getAuth');
        alert(authData);
        console.log(authData);
    });
    $('#login').on('click', function(e){
        rootRef.authWithOAuthPopup("facebook", function(err, authData){
            if(err && err.code === 'TRANSPORT_UNAVAILABLE'){
                rootRef.authWithOAuthRedirect("facebook", function(err, authData){
                    if(authData){
                        alert('redirect');
                        alert(authData);

                    }
                })   
            }
        })    
     });

在此处链接http://jsfiddle.net/blackbing/zjunuzec/5/ 了解更多详情。

它适用于 Safari IOS。如果登录成功,会提示[object],但在IOS的Chrome中显示为null。

有什么想法吗?

【问题讨论】:

    标签: javascript ios google-chrome firebase


    【解决方案1】:

    我想我弄清楚了问题所在。

    首先,据说getAuth在文档中是同步的,但是在authWithOAuthRedirect上同步是不可靠的。所以当页面重定向到原始页面时。我无法获取 authData,因此无法确定用户是否正在登录。 (但实际上用户已登录)。 其次,由于无法调用“authWithOAuthRedirect”中的回调,因此回调函数无论如何也无法获取authData。如果发生错误,它可以被调用,对吗?我建议注意文档中的这种行为。

    无论如何:我认为文档中的 sn-p 必须更正: https://www.firebase.com/docs/web/guide/user-auth.html#section-popups

    authWithFunction 中的回调是处理错误的,不建议处理 authData,onAuth 是更好的获取 authData 的方法。我在我的 gist 上更新了一个 sn-p [https://gist.github.com/blackbing/f77d04cbed4b0059af2e]

    var ref = new Firebase("https://<your-firebase>.firebaseio.com");
    
    
    rootRef.onAuth( function(authData){
      //It is a better way to get authData instead of get from auth callback function
      console.log(authData);
    });
    // prefer pop-ups, so we don't navigate away from the page
    // auth callback is to handle if occur error
    ref.authWithOAuthPopup("google", function(err) {
      if (err) {
        if (err.code === "TRANSPORT_UNAVAILABLE") {
          // fall-back to browser redirects, and pick up the session
          // automatically when we come back to the origin page
          ref.authWithOAuthRedirect("google", function(err) { ... });
        }
      }
    });
    

    顺便说一句,我发现了一个类似的问题https://stackoverflow.com/a/26416696/797411,解决方法不好,但我认为我们遇到了同样的问题。

    【讨论】:

      猜你喜欢
      • 2016-04-22
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      相关资源
      最近更新 更多