2017-09-06 52 views
0

我正在使用社交媒體帳戶進行Firebase身份驗證。註冊後,我無法註銷。使用Firebase註銷

以下是我的Javascript代碼:

<script src="https://www.gstatic.com/firebasejs/4.1.3/firebase.js"></script> 
<script> 
    // Initialize Firebase 
    var config = { 
    apiKey: "*******", 
    authDomain: "******", 
    databaseURL: "******", 
    projectId: "*******", 
    storageBucket: "******", 
    messagingSenderId: "*******" 
    }; 
    firebase.initializeApp(config); 

    firebase.auth().onAuthStateChanged(function(user) { 
     if (user) { 
     // User is signed in. 
     var displayName = user.displayName; 
     var email = user.email; 
     var photoURL = user.photoURL; 
     user.getToken().then(function(accessToken) { 

      console.debug('user', user); 
      document.getElementById('sign-in-status').textContent = 'Signed in'; 
      document.getElementById('name').textContent = JSON.stringify(displayName) 
      document.getElementById('email').textContent = JSON.stringify(email) 
      document.getElementById('account-details').textContent = JSON.stringify({ 
       displayName: displayName, 
       email: email, 
       photoURL: photoURL 
      }); 
     }); 

     } else { 
      console.log('not logged in'); 
     /*document.getElementById('sign-in-status').textContent = 'Signed out'; 
     document.getElementById('sign-in').textContent = 'Sign in'; 
     document.getElementById('account-details').textContent = 'null'; 
     */ 
     } 

    }); 

    // User is signed out. 
     firebase.auth().signOut().then(function() { 
     // Sign-out successful. 
     signOutSuccessUrl: 'https://url.html' 
     }).catch(function(error) { 
     // An error happened. 
     }); 

</script> 

而在HTML我被稱之爲: <button class="btn btn-primary" onclick="signOut()"><i class="fa fa-sign-out">/i> Log out</button>

但在點擊按鈕沒有任何反應。

我甚至試圖把signout段放在函數中,但是沒有幫助。

任何形式的幫助將不勝感激。謝謝。

回答

1

如果您的點擊事件處理程序調用signOut方法,那麼您必須在某處定義它。像:

window.signOut = function signOut(_e) { 
    firebase.auth().signOut().then(function() { 
    // Sign-out successful. 
    }).catch(function(error) { 
    // An error happened. 
    }); 
} 
+0

我插入你的代碼,然後在控制檯中檢查。 「未登錄」正在顯示,但它沒有被重定向到我在網址中提到的頁面。 window.signOut = function signOut(_e)firebase.auth()。signOut()。then(function(){//退出成功。 signOutURL:'https://url.html'; })。catch(function(error){ //發生錯誤。 }); }' –

+1

如果你想在成功登出後重定向,那麼你可以在你的'signOut.then'回調中添加'window.location.replace(「https://url.html」)'。 – user2520818

+0

謝謝!此外,如果您看到我的代碼,我也嘗試檢索用戶的圖像。在所有使用過PhotoUrl的例子中。因此只會顯示網址。我應該做些什麼才能看到照片? –