Search code examples
javascriptfirebasefirebase-authenticationgoogle-oauth

How to add SignOut option in firebase


First of all, I'm sorry if this has already been asked but I searched a lot and couldn't find anything. Basically, I have this code that lets me log in but I can't log out, I searched and tried different codes but it didn't work. Could anyone please help.

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>

I just want to know how can I implement the signout option.


Solution

  • To sign the user out of Firebase Authentication, call firebase.auth().signOut(). See for a longer example the bottom of this page: https://firebase.google.com/docs/auth/web/password-auth.

    Note that this doesn't sign the user out of Google Authentication. If you want to do that too (you typically won't), see https://developers.google.com/identity/sign-in/web/sign-in#sign_out_a_user