Google身份验证无限循环

时间:2017-02-05 21:52:43

标签: 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上,我完全迷失了。我创建了一个带有谷歌登录按钮的弹出窗口。

               <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,但我迷失了。我的代码在哪里执行这些步骤?例如,我是否包括&#34; var provider = new firebase.auth.GoogleAuthProvider();&#34;在我的index.html页面的脚本中?在app.js?当然我需要我的按钮重定向到这个登录功能,所以我该怎么做?

1 个答案:

答案 0 :(得分:1)

你不能在同一个按钮点击回调中调用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.
  }
});
相关问题