【问题标题】:Firebase: Google OAuth Infinite RedirectsFirebase:Google OAuth 无限重定向
【发布时间】:2015-01-13 16:46:15
【问题描述】:

我对 Firebase 还很陌生。我正在尝试将 Google OAuth 连接到我的 Firebase 实例。

我设置了所有内容并获取了客户端 ID 和客户端密码。我将 localhost 添加到 Firebase 仪表板的白名单中。然后我使用了下面的 Firebase 示例:

<html>
<head>
  <script src="https://cdn.firebase.com/js/client/2.0.4/firebase.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<script>

  var ref = new Firebase("https://<firebase url>.firebaseio.com");
  ref.authWithOAuthRedirect("google", function(error, authData) {
    if (error) {
      console.log("Login Failed!", error);
    } else {
      console.log("Authenticated successfully with payload:", authData);
    }
  });

</script>
</body>
</html>

当我打开它时,它会要求通过 Google 进行身份验证。当我接受它时,它只是继续进行重定向(无限)并且没有完成加载。对这个问题的任何见解都会有所帮助。谢谢。

编辑: 我注意到:authWithOAuthPopup() 方法有效,但重定向只是卡在无限重定向循环中。

【问题讨论】:

  • 你能在 JSFiddle/JSBin 中重现这个吗?
  • 在 JSFiddle 我得到:{“错误”:“请使用 POST 请求”}

标签: redirect oauth firebase infinite


【解决方案1】:

任何时候调用ref.authWithOAuthRedirect(...),都是在告诉 Firebase 启动基于重定向的身份验证流程并将浏览器重定向到 OAuth 提供程序。调用此方法将始终尝试创建一个 new 会话,即使浏览器中已经存在一个会话。

如果一个新的登录会话尚不存在,仅尝试创建一个新的登录会话,请尝试以下使用onAuth(...) 事件监听器:

var ref = new Firebase("https://<firebase url>.firebaseio.com");
ref.onAuth(function(authData) {
  if (authData !== null) {
    console.log("Authenticated successfully with payload:", authData);
  } else {
    // Try to authenticate with Google via OAuth redirection
    ref.authWithOAuthRedirect("google", function(error, authData) {
      if (error) {
        console.log("Login Failed!", error);
      }
    });
  }
})

【讨论】:

  • 太棒了!谢谢你。我想我已经足够了解 javascript,但仍然不熟悉所有不同的 Firebase 方法。这很好用。
猜你喜欢
  • 2019-11-10
  • 1970-01-01
  • 1970-01-01
  • 2016-12-22
  • 1970-01-01
  • 1970-01-01
  • 2017-04-10
  • 2017-04-07
  • 2016-12-18
相关资源
最近更新 更多