【问题标题】:Signing out using Firebase使用 Firebase 退出
【发布时间】:2017-09-06 08:53:14
【问题描述】:

我正在通过社交媒体帐户使用 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 中,我通过以下方式调用它: &lt;button class="btn btn-primary" onclick="signOut()"&gt;&lt;i class="fa fa-sign-out"&gt;/i&gt; Log out&lt;/button&gt;

但是在单击按钮时没有任何反应。

我什至尝试将注销段放在一个函数中,但这并没有帮助。

我们将不胜感激任何形式的帮助。谢谢。

【问题讨论】:

    标签: javascript firebase firebase-authentication


    【解决方案1】:

    如果您的点击事件处理程序调用signOut 方法,那么您必须在某处定义它。喜欢:

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

    【讨论】:

    • 我插入了您的代码,然后在控制台中进行了检查。正在显示“未登录”,但它没有被重定向到我在 url 中提到的页面。 window.signOut = function signOut(_e) { firebase.auth().signOut().then(function() { // Sign-out successful. signOutURL: 'https://url.html'; }).catch(function(error) { // An error happened. }); }
    • 如果您想在成功退出后重定向,那么您可以将window.location.replace("https://url.html") 添加到您的signOut.then 回调中。
    • 谢谢!此外,如果您看到我的代码,我也在尝试检索用户的图像。在我看到的所有示例中都使用了 photoUrl。因此,结果只显示了 url。我应该做些什么改变才能真正看到照片?
    • 如果您有图像的 url,那么您可以简单地渲染/更新图像标签。 document.getElementById("avatar").src = photoUrl;
    • 谢谢!您的两个解决方案都有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 2017-03-17
    • 2016-12-21
    • 1970-01-01
    相关资源
    最近更新 更多