Firebase:Google OAuth无限重定向 [英] Firebase: Google OAuth Infinite Redirects

查看:147
本文介绍了Firebase:Google OAuth无限重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Firebase相当陌生。我尝试将Google OAuth连接到我的Firebase实例。



我设置了一切,并获得客户端ID和客户端密码。我将本地主机添加到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 successful with payload:,authData);
}
});

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

当我打开它时,它会要求与Google进行身份验证。当我接受它,它只是继续做重定向(无限),并没有完成加载。任何有关问题的见解都会有所帮助。感谢。
$ b $编辑:
我注意到:authWithOAuthPopup()方法的工作原理,但重定向只是卡在一个无限的重定向循环。

解决方案

Firebase启动基于重定向的身份验证流程,并将浏览器重定向到OAuth提供程序。调用这个方法总是会尝试创建一个新的会话,即使已经在浏览器中持久化了。



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

  var ref = new Firebase(https://< firebase url> .firebaseio.com); 
ref.onAuth(authData){
if(authData!== null){
console.log(Authenticated successful with payload:,authData);
} else {
//尝试通过OAuth重定向与Google进行身份验证
ref.authWithOAuthRedirect(google,function(error,authData){
if(error){
console。 log(Login Failed!,error);
}
});
}
})


I'm fairly new to Firebase. I'm trying to hook up Google OAuth to my Firebase instance.

I set everything up and got the client ID and client secrete. I added localhost to the whitelist in the Firebase dashboard. I then used the Firebase example below:

<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>

When I open it, it asks permission to authenticate with Google. When I accept it, it just keeps doing redirects (infinity) and doesn't finish loading. Any insight on the problem will be helpful. Thanks.

Edit: I've noticed that: authWithOAuthPopup() method works but the redirect just stuck in an infinite redirect loop.

解决方案

Any time you call ref.authWithOAuthRedirect(...), you're telling Firebase to initiate the redirect-based authentication flow and redirect the browser to the OAuth provider. Calling this method will always attempt to create a new session, even if one is already persisted in the browser.

To only attempt creation of a new login session if one doesn't already exist, try the following which makes use of the onAuth(...) event listener:

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);
      }
    });
  }
})

这篇关于Firebase:Google OAuth无限重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆