Firebase JS Error: getToken aborted due to token change

折月煮酒 提交于 2021-02-07 13:28:38

问题


I'm getting a Firebase error "Error: getToken aborted due to token change" while running Firestore transaction using the JavaScript library. The error doesn't get thrown every time and I couldn't find the pattern. I suppose I've implemented some race conditions somewhere.

The user flow in my app goes like this:

  1. Register a new account & submit an additional string in the same form
  2. Log user in after registration using the same credentials
  3. After log in, take that additional string and save it to Firestore (in a transaction).
  4. Transaction fails due to Error: getToken aborted due to token change.

The flow of promises:

    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    
      })    
    }

The database transaction call

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 });
  });
});

I couldn't find the error anywhere on google.

I'm thinking the error is caused by the token change that happens at log in. On the other hand I'm reading that Firebase accepts old tokens for a few more moments. Any thoughts?


回答1:


I got a similar error[error code] while debugging my client which was connecting to firebase via a React App.

The solution was

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read;
      allow write: if false;
    }
  }
}

Putting the above inside the rules part of the firestore settings which apparently means you need to allow reads for external apis but writes are blocked and it was previously blocking both reads and writes.

This could be one of the issues if you are trying to read from your client/server

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read;
      allow write;
    }
  }
}
  • DISCLAIMER: * I am not an Expert at firebase. I am not sure if this would compromise your DB to be written by external apis since you are opening your firestore firewall by doing this

P.S. there is a firebase-admin module which I think helps in doing writes by handling authentication in a separate fashion. I think that module is more suited for writes and the normal firebase.firestore(app) is for reads.



来源:https://stackoverflow.com/questions/52513691/firebase-js-error-gettoken-aborted-due-to-token-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!