【问题标题】:Switching HTML Pages (Not Asynchronously) after Google Firebase authentication在 Google Firebase 身份验证后切换 HTML 页面(非异步)
【发布时间】:2017-06-25 02:27:53
【问题描述】:

我正在尝试创建一个网站,在该网站上单击一个按钮以使用 google 登录,然后转到一个新的 html 页面。

但是,当新的 google 登录窗口弹出时,它立即关闭,html 页面发生变化

  • 我确实希望更改 html 页面,但是:我需要 google 登录吗
    先上班?

  • 有没有办法等到用户登录后再转到新页面?

我在想是否有一个布尔方法来查看登录是否成功,但我找不到。

你能帮帮我吗?

    <html>
      <head>
//connect to css and javascript file
        <link rel="stylesheet" href="hackathon.css">
        <script src=scripts.js></script>
      </head>

      <script src="https://www.gstatic.com/firebasejs/4.1.3/firebase.js"></script>
      <script>
      // Initialize Firebase
      var config = {
        apiKey: "AIzaSyAOWTFIhpydI27RMa_JzOW0Ffyajb9sTqg",
        authDomain: "teach-and-learn-cd50e.firebaseapp.com",
        databaseURL: "https://teach-and-learn-cd50e.firebaseio.com",
        projectId: "teach-and-learn-cd50e",
        storageBucket: "",
        messagingSenderId: "412721016471"
      };
      firebase.initializeApp(config);
      </script>

      <br><br><br><br><br><br><br><br><br>
//create the header

<p><center><font face="Verdana" size="7" color="white">Teach and Learn</font></p>
    //login button
      <button class="button1">Log In</button>
    //google sign in 
      <script type="text/javascript">
        button=document.querySelector('button')
        database=firebase.database()
        reference=database.ref()
        button.onclick=function(){
        var provider = new firebase.auth.GoogleAuthProvider();

        firebase.auth().signInWithPopup(provider).then(function(result) {
           var token = result.credential.accessToken;
           var user = result.user;
           }).catch(function(error) {
           var errorCode = error.code;
           var errorMessage = error.message;
           var email = error.email;
           var credential = error.credential;
        })

         //immediately goes to this page without giving the use 
         //opportunity to sign in
        //main.html is loaded and the sign in window is closed
        window.location.href="./main.html";

      </script>
    </html>

【问题讨论】:

  • 你可能已经登录了谷歌?

标签: javascript html firebase-authentication google-authentication


【解决方案1】:

如果登录成功,function(result) 里面的代码就会运行。如果登录失败,function(error) 内部的代码将运行。 then 中的任何内容都会等到您尝试登录。把重定向代码放在then里面。

我在此 CodePen 上修改了您的代码,因此您可以自己尝试一下。删除代码中的第二个window.location.href = "./main.html"; 非常重要。

https://codepen.io/anon/pen/MoOeyv?editors=1000

【讨论】:

  • 这不起作用。它给了我这个 Uncaught SyntaxError: Unexpected end of input.
  • 看看我的编辑。我设置了一个 CodePen,你可以试试看。
【解决方案2】:

将 window.location.href 移动到回调中?

firebase.auth().signInWithPopup(provider).then(function(result) {

    //..
    window.location.href="./main.html";

}).catch(function(error) {
    //..
});

【讨论】:

  • 这给了我一个 Uncaught SyntaxError: Unexpected end of input
  • @RandomPerson 请通过 jsfiddle 发送您的更新代码。
猜你喜欢
  • 2020-03-06
  • 1970-01-01
  • 2016-09-26
  • 2022-01-11
  • 2021-08-01
  • 2013-02-02
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
相关资源
最近更新 更多