javascript - 谷歌认证死循环

标签 javascript firebase firebase-authentication

我的 app.js 看起来像这样:

// Initialize Firebase
    const config = {
        apiKey: //...
        authDomain: //...
        databaseURL: //...
        storageBucket://...
        messagingSenderId: //...
     };
     firebase.initializeApp(config);

}());

    function.googleSignin(){
        var provider = new firebase.auth.GoogleAuthProvider();
        firebase.auth().signInWithRedirect(provider);
    }

    firebase.auth().signInWithPopup(provider).then(function(result) {
  // This gives you a Google Access Token. You can use it to access the Google API.
        var token = result.credential.accessToken;
  // The signed-in user info.
        var user = result.user;
  // ...
}).catch(function(error) {
  // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
  // The email of the user's account used.
      var email = error.email;
  // The firebase.auth.AuthCredential type that was used.
      var credential = error.credential;
  // ...
});

但是,现在当我加载页面时,它会自动重定向到谷歌登录(目前,我只希望它们在单击按钮时重定向到登录)。更重要的是,它会无限循环地执行此操作:在我登录后将我重定向到登录页面。

我需要做什么才能使其仅在单击按钮时重定向?并且之后不会自动重定向到谷歌登录?

================================================== =========================== 我对网络开发非常陌生,对于如何将谷歌登录集成到我托管在 Firebase 上的应用程序中完全迷失了。我创建了一个带有 Google 登录按钮的弹出窗口。

               <div id="id01" class="modal">
               <form class="modal-content animate" action="action_page.php">
                   <div class="imgcontainer">
                       <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">&times;</span>
                        </div>
                       <div class="loginMsg">
                           <p>hi there!</p>
                           <p>log in to post and comment on ride share</p>
                       </div>
                       <button class="loginBtn loginBtn--facebook">connect with facebook</button>
                       <button class="loginBtn loginBtn--google">connect with google     </button>
                </form>
           </div>

我试图理解 Firebase instructions ,但我迷路了。我在代码中的什么位置执行这些步骤?例如,我是否包含“var provider = new firebase.auth.GoogleAuthProvider();”在我的index.html 页面的脚本中?在 app.js 中?当然,我需要按钮来重定向到此登录功能,那么我该怎么做呢?

最佳答案

您不能在同一个按钮点击回调中调用signInWithRedirect和getRedirectResult。

通常,您可以执行以下操作:

对于点击处理程序上的按钮,请执行以下操作:

function.googleSignin(){
  var provider = new firebase.auth.GoogleAuthProvider();
  firebase.auth().signInWithRedirect(provider);
}

在函数之外设置重定向监听器:

firebase.auth().getRedirectResult().then(function(result) {
  if (result.user) {
    // User just signed in. you can get the result.credential.
    // Update your UI, hide the sign in button.
  } else if (firebase.auth().currentUser) {
    // User already signed in.
    // Update your UI, hide the sign in button.
  } else {
    // No user signed in, update your UI, show the sign in button.
  }
});

关于javascript - 谷歌认证死循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42057767/

相关文章:

android - Firebase 用户更改电子邮件或电话号码

javascript - Nodejs 模块实现

javascript - 如何让元素触发一个接一个地出现,间隔很小?

node.js - Firebase 生成圆形缩略图失败,代码为 1

firebase - 使用 Firebase 电话号码身份验证和 react native

android - 检索已使用 Firebase Auth 注册的用户列表

android - 如何在用户注册期间将数据存储在 firebase 的实时数据库中?

javascript - 当 URL 中包含 Angularjs 数据时,如何加密我的 URL?

javascript - 传单 map 未加载

android - Firebase 创建推送两次 android