Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase JS Error: getToken aborted due to token change

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?

like image 836
Gogi Avatar asked Sep 26 '18 08:09

Gogi


1 Answers

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.

like image 199
neolegolas Avatar answered Nov 15 '22 01:11

neolegolas