Firebase Google登录无法正常工作

时间:2017-11-22 02:40:27

标签: firebase firebase-authentication firebaseui firebasesimplelogin

我已在这样的代码中签名:

<!DOCTYPE html>
<html lang="en">
   <head>
      <title>Gmail Sign In</title>

<script src="https://www.gstatic.com/firebasejs/4.6.2/firebase.js"></script>
<script>
  // Initialize Firebase
  var config = {
    apiKey: "AIzaSyC04no7Ge4ku9xQI3bjJknjhUK9W3UuoiE",
    authDomain: "sb-sign.firebaseapp.com",
    databaseURL: "https://sb-sign.firebaseio.com",
    projectId: "sb-sign",
    storageBucket: "sb0!-sign.appspot.com",
    messagingSenderId: "431178337718"
  };
  firebase.initializeApp(config);
   window.onload = login();
  function login(){
    firebase.auth().onAuthStateChanged(newLoginHappend);
    function newLoginHappend(user){
        if (user){
            //user has signed in
            app(user);
            document.getElementById("SignOutButton").style = "display:block";
        }
        else{
            document.getElementById("SignInButton").style = "display:block";
            document.getElementById("ClientName").innerHTML = there! please login.;

  }
  function app(user){
  //user.displayName    
  //user.email
  //user.photoURL
  //user.uid
document.getElementById("ClientName").innerHTML = user.displayName;
  }

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


  }



</script>
   </head>
   <body>
     <h1>
         Hello, <span id="ClientName"></span>
         <button id="SignInButton" style="display:none" onclick='SignIn()'>Sign In</button> <button id="SignOutButton" style="display:none" onclick='firebase.auth().signOut();window.refresh();'>Sign Out</button>
     </h1>
   </body>
</html>

当正文加载时,如果用户未登录,则需要显示登录按钮(#SignInButton),隐藏退出按钮并显示文本输出,说“你好!请登录”。当用户点击该按钮时,我想要显示登录页面。 在正文加载时,如果用户已经登录,我想显示文本,说“你好”。而且,我想隐藏登录按钮并显示退出按钮。 但是当我打开这个页面时,我只是在屏幕上看到一个文本“Hello”。没有其他显示。也没有登录按钮也没有登出按钮。 请帮忙。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

您的javascript包含语法错误:

SyntaxError: missing ; before statement firebase.html:29:60

enter image description here enter image description here

这意味着应该像这样引用字符串:

document.getElementById("ClientName").innerHTML = 'there! please login.';

或者像这样:

document.getElementById("ClientName").innerHTML = "there! please login.";

答案 1 :(得分:0)

我找到了自己的答案。 我做了一些代码重写和一些更改。 现在它正常运作。 这是新代码:

<!DOCTYPE html>
<html lang="en">
   <head>
      <title>Gmail Sign In</title>

<script src="https://www.gstatic.com/firebasejs/4.6.2/firebase.js"></script>
<script>
  // Initialize Firebase
  var config = {
    apiKey: "AIzaSyC04no7Ge4ku9xQI3bjJknjhUK9W3UuoiE",
    authDomain: "sb-sign.firebaseapp.com",
    databaseURL: "https://sb-sign.firebaseio.com",
    projectId: "sb-sign",
    storageBucket: "sb-sign.appspot.com",
    messagingSenderId: "431178337718"
  };
firebase.initializeApp(config);
  function login(){
    function newLoginHappend(user){
        if (user){
            //user has signed in
            app(user);
document.getElementById("SignOutButton").style = "display:block";
document.getElementById("SignInButton").style = "display:none";
        }
        else{       }
    }
firebase.auth().onAuthStateChanged(newLoginHappend);


  }
  function login2(){
    function newLoginHappend(user){
        if (user){
            //user has signed in
            app(user);
document.getElementById("SignOutButton").style = "display:block";
        }
        else{
            var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithRedirect(provider);
        }
    }
firebase.auth().onAuthStateChanged(newLoginHappend);
  }
  function app(user){
  //user.displayName    
  //user.email
  //user.photoURL
  //user.uid
document.getElementById("ClientName").innerHTML = user.displayName;
  }
  window.onload = login();
</script>
   </head>
   <body>
     <h1>
         Hello, <span id="ClientName"></span> 
         </h1>
         <button id="SignInButton" style="display:block" onclick='login2()'>Sign In</button>    <button id="SignOutButton" style="display:none" onclick='firebase.auth().signOut();location.reload(true);'>Sign Out</button>

   </body>
</html>