Firebase JS 错误:由于令牌更改,getToken 中止

Gog*_*ogi 6 firebase firebase-authentication google-cloud-firestore

使用 JavaScript 库运行 Firestore 事务时,我收到 Firebase 错误“错误:由于令牌更改中止getToken” 。错误不会每次都抛出,我找不到模式。我想我已经在某处实施了一些竞争条件。

我的应用程序中的用户流程是这样的:

  1. 注册一个新帐户并以相同的形式提交额外的字符串
  2. 注册后使用相同的凭据登录用户
  3. 登录后,获取该附加字符串并将其保存到 Firestore(在事务中)。
  4. 由于交易失败 Error: getToken aborted due to token change.

承诺流程:

    firebase.auth().createUserWithEmailAndPassword(email, password)
      .catch(signupError => {
        // no problems here
      })
      .then(() => {
        return firebase.auth().signInWithEmailAndPassword(email, password)
      })
      .catch(loginError => {
        // no problem here
      })
      .then((user) => {
        // Database write call which fails (see lower code block)
        return this.claimInput.writeClaimedPlace(user.user.uid, claimedPlace);
      })
      .catch(error => {
        // "getToken aborted" ERROR GETS CAUGHT HERE, transaction fails    
      })    
    }
Run Code Online (Sandbox Code Playgroud)

数据库事务调用

firestore.runTransaction(function(transaction) {

  return transaction.get(usersBusinessRef).then(function(usersBusinesDoc) {

    let claimedPlaces = [];
    if (usersBusinesDoc.exists && usersBusinesDoc.data().claimedPlaces) {
      claimedPlaces = usersBusinesDoc.data().claimedPlaces;
    }
    claimedPlaces.push(claimedPlace);

    return transaction.set(usersBusinessRef, { claimedPlaces }, { merge: true });
  });
});
Run Code Online (Sandbox Code Playgroud)

我在谷歌的任何地方都找不到错误。

我认为错误是由登录时发生的令牌更改引起的。另一方面,我读到 Firebase 接受旧令牌一段时间。有什么想法吗?

neo*_*las 1

在调试通过 React 应用程序连接到 firebase 的客户端时,我遇到了类似的错误[错误代码]。

解决方案是

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read;
      allow write: if false;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

将上述内容放在 firestore 设置的规则部分中,这显然意味着您需要允许读取外部 api,但写入会被阻止,并且它之前会阻止读取和写入。

如果您尝试从客户端/服务器读取数据,这可能是问题之一

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read;
      allow write;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)
  • 免责声明: *我不是 firebase 专家。我不确定这是否会损害您由外部 api 编写的数据库,因为您通过这样做打开了 firestore 防火墙

PS,我认为有一个firebase-admin模块可以通过以单独的方式处理身份验证来帮助进行写入。我认为该模块更适合写入,而普通模块firebase.firestore(app)更适合读取。