首页 > 解决方案 > 如何在 Firebase 中添加 SignOut 选项

问题描述

首先,很抱歉,如果已经问过这个问题,但我搜索了很多,但找不到任何东西。基本上,我有这段代码可以让我登录但我无法注销,我搜索并尝试了不同的代码但它没有用。谁能帮忙。

initApp = function() {
        firebase.auth().onAuthStateChanged(function(user) {
          if (user) {
            // User is signed in.
            var displayName = user.displayName;
            var email = user.email;
            var emailVerified = user.emailVerified;
            var photoURL = user.photoURL;
            var uid = user.uid;
            var phoneNumber = user.phoneNumber;
            var providerData = user.providerData;
            document.getElementById("name-container").innerHTML=displayName;
          } else {
            // User is signed out.
            
          }
        }, function(error) {
          console.log(error);
        });
      };

      window.addEventListener('load', function() {
        initApp()
      });
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Login</title>
    <style type="text/css">
      @import url('https://fonts.googleapis.com/css?family=Dosis');
      body{
        margin: 0;
        font-family: 'Dosis', sans-serif;
      }

      #header{
        width: 100%;
        height: 100px;
        padding-top: 50px;
        text-align: center;
        color: black;
        font-size: 34px;
        box-shadow: 10px 10px 5px #aaaaaa;
      }
      h2{
        margin: 0;
      }
      .container{
        width: 100%;
        text-align: center;
        margin-top: 90px;
      }
      .first{
        display: table;
        margin: 0 auto;
        text-align: center;
        padding: 20px;

      }
      a{
        text-decoration: none;
        font-size: 30px;
        color: red;
      }
      a:hover{
        color: black;
      }
      p{
        text-decoration: none;
        font-size: 30px;
        color: red;
      }
      p:hover{
        color: black;
         cursor: pointer;
      }
      footer{
        text-align: center;
        border-top: 1px solid black;
        padding: 20px;
        position: fixed;
        bottom: 0;
        left: 0
        right:0;
        width: 100%;
      }

    </style>
    <script src="https://www.gstatic.com/firebasejs/5.5.7/firebase.js"></script>
   
    <script type="text/javascript" src="app.js"></script>
  </head>
  <body>
   <div id="header">
      
        <h2>Welcome <span id="name-container"></span></h2>

    </div>

    <div class="container">
       
        <div class="first">
        
          <a href="">Account</a>

        </div>
        <div class="first">
        
          <a href="">My Files</a>

        </div>
        <div class="first">
        
          <p class="first">LogOut</p>

        </div>


    </div>
    <footer>
      
      Smarter © 2018

    </footer>
  </body>
</html>

我只想知道如何实现注销选项。

标签: javascriptfirebasefirebase-authenticationgoogle-oauth

解决方案


要让用户退出 Firebase 身份验证,请调用firebase.auth().signOut()。请参阅此页面底部的更长示例:https ://firebase.google.com/docs/auth/web/password-auth 。

请注意,这不会让用户退出 Google 身份验证。如果您也想这样做(通常不会),请参阅https://developers.google.com/identity/sign-in/web/sign-in#sign_out_a_user


推荐阅读