使用 Firebase 匿名身份验证是否允许我确保我的 Firestore 数据和云函数只能由我的应用程序访问?

dak*_*ojo 5 firebase google-cloud-functions

我正在创建一个没有任何理由让用户进行身份验证的移动应用程序。但是,我不希望其他人编写可以访问我在 Firestore 中的数据或调用我的任何云函数的应用程序或网站。

这是否意味着我需要实施匿名身份验证,然后编写要求请求来自经过身份验证的用户的访问规则?

或者有没有办法编写规则来说明它们必须来自我的应用程序?

Vic*_*man 1

我们遇到了同样的问题:一个要部署到很多人的应用程序,它需要只能从 firestore 文档中读取,但另一个管理应用程序(实际上只是一个网页)不会被分发,需要能够写入到那些文件。

我们的解决方案是:

  • 为数据库创建另一个用户,使用电子邮件和密码身份验证。(我们不允许用户创建帐户 - 仅拥有我们创建的一个静态电子邮件/密码。)
  • 常规应用程序仅使用匿名登录
  • 有“管理应用程序”的电子邮件/密码登录。
  • 为每个文档添加如下规则:
      allow read;
      allow write: if request.auth.uid == 'notshowingyououridhere-sorry';
Run Code Online (Sandbox Code Playgroud)

我们使用 ionic 和 typescript,因此执行用户/密码登录的代码相对简单:

      firebase.initializeApp(credentials);
      firebase.auth()
        .signInWithEmailAndPassword('obfuscated@sorry.com', 'sorrynotshowingyouourpassword')
        .catch(err => {
          console.log('Something went wrong:', err.message);
        });


      firebase.auth().onAuthStateChanged((user) => {
        if (user) {
          // User is signed in.
          const isAnonymous = user.isAnonymous;
          const uid = user.uid;
          console.log('onAuthStatChanged: isAnon', isAnonymous);
          console.log('userid', user.uid);
        } else {
          console.log('onAuthStateChanged: else part...');
        }
      });
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。