【问题标题】:Checking user authentication with Firebase使用 Firebase 检查用户身份验证
【发布时间】:2019-01-25 12:48:31
【问题描述】:

我正在使用 Firebase 身份验证将用户登录到我的网站。登录后,它们会被重定向到 /console.html。

在这个文件的头部我有

<script type="text/javascript">
    firebase.auth().onAuthStateChanged(function(user) {
      if (user) {
        // User is signed in.
      } else {
        // No user is signed in.
        window.location.href = '/';
      }
    });
  </script>

检查他们是否已登录。如果没有,他们将被重定向到 index.html,这只是一个登录页面。我遇到的问题是,如果有人禁用 Javascript 并转到 /console.html,这将被忽略,他们能够看到该网页上显示的任何内容。

用于提供登录的代码使用 FirebaseUI,如下:

  <script type="text/javascript">
    // Initialize the FirebaseUI Widget using Firebase.
    var ui = new firebaseui.auth.AuthUI(firebase.auth());

    var uiConfig = {
      callbacks: {
        signInSuccessWithAuthResult: function(authResult, redirectUrl) {
          // User successfully signed in.
          // Return type determines whether we continue the redirect automatically
          // or whether we leave that to developer to handle.
          return true;
        },
        uiShown: function() {
          // The widget is rendered.
          // Hide the loader.
          document.getElementById('loader').style.display = 'none';
        }
      },
      // Will use popup for IDP Providers sign-in flow instead of the default, redirect.
      signInFlow: 'popup',
      signInSuccessUrl: 'console.html',
      signInOptions: [
        // Leave the lines as is for the providers you want to offer your users.
        firebase.auth.EmailAuthProvider.PROVIDER_ID
      ],
      // Terms of service url.
      tosUrl: '<your-tos-url>',
      // Privacy policy url.
      privacyPolicyUrl: '<your-privacy-policy-url>'
    };

    // The start method will wait until the DOM is loaded.
    ui.start('#firebaseui-auth-container', uiConfig);
  </script>

(从 Firebase 文档复制)。

【问题讨论】:

    标签: javascript html firebase firebase-authentication


    【解决方案1】:

    您永远不应该只保护客户端的任何重要信息,在决定您页面上的内容是否安全时,请始终假设您的客户端保护不存在。

    您在 HTML 文件中显示的任何内容都应仅从服务器/数据库/无论服务器端的某些安全规则匹配的任何内容中获取。

    【讨论】:

    • 谢谢!很抱歉让您感到痛苦,但是您对如何使用 Firebase 执行此操作有任何指导吗?这是我第一次尝试向网站添加身份验证等。
    • @chump52 当然,console.html 中的数据是存储在数据库还是什么的?
    • 是的,它将存储在链接到项目的 Firebase 数据库中 - 现在考虑一下,我认为如果用户没有登录,它将不允许读取数据,这样可能会解决问题(?)
    • @chumps52 那么您需要在 Firebase 控制台中定义安全规则,如果请求来自经过身份验证的用户,则仅允许读取相关集合
    • 好的,我想我知道该怎么做/可以弄清楚。感谢您的帮助!
    猜你喜欢
    • 2017-12-05
    • 2021-07-31
    • 1970-01-01
    • 2022-12-12
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多