Reputation: 1222
I have an app with Firebase where a user logs into their account. The only problem is, when I want to log out, I want to erase some info I have previously saved on the database, but for some reason the database doesn't get updated and this warning message appears:
Error: [database/permission denied] Client doesn't have permission to acess the desired data
My guess is that that's happening because the update code doesn't have enough time to execute the instruction. My code looks like this (it's triggered by a button press):
onPressLogout = () => {
const { navigation } = this.props;
const userId = auth().currentUser.uid;
database().ref(`users/${userId}`).update({ tokens: '' });
auth().signOut().then(() => navigation.navigate('Auth'));
}
Any help's appreciated.
Upvotes: 0
Views: 121
Reputation: 65
React is asynchronous, so it doesn't wait for line to be executed in 100% and instead it executes further code. Usege of await
should do the job:
async onPressLogout(){
const { navigation } = this.props;
const userId = auth().currentUser.uid;
await database().ref(`users/${userId}`).update({ tokens: '' });
auth().signOut().then(() => navigation.navigate('Auth'));
}
Upvotes: 0
Reputation: 598837
It seems the signout happens before the update
operation, which then fails because the user is no longer signed in.
Easiest is to wait until the write is completed before signing out. You can do this either with await
:
await database().ref(`users/${userId}`).update({ tokens: '' });
auth().signOut().then(() => navigation.navigate('Auth'));
Or with a more classic then()
:
database().ref(`users/${userId}`).update({ tokens: '' }).then(()=> {
auth().signOut().then(() => navigation.navigate('Auth'));
})
Upvotes: 1