Reputation: 169
I have an app (Ionic Angular Capacitor 2) calling some object in a Firebase realtime database. It have been working fine with access rules and anonymous auth. But after some modules updating (Don't know if thats the problem) i get permission denied when calling the db. This is happening in both web and on ios/android. I've read all pages from google and here. But mostly the problem is firestore rules on Realtime db or solved by setting .read/.write rules to true.
This is the auth called on OnInit() when user is created or logged in, continueLoad() is called:
async loginAndLoad() {
// create anonymous user if not exists and/or log in
await this.fireauth.signInAnonymously().catch((error) => {
console.log('Login error: ' + error.code);
console.log('Login error message: ' + error.message);
});
// trigger state change, when user is logged in
this.fireauth.onAuthStateChanged((user) => {
if (user.isAnonymous) {
var isAnonymous = user.isAnonymous;
var uid = user.uid;
console.log('User added: ' + isAnonymous);
console.log('User uid: ' + uid);
// We have a signed in user, go on.. Else throw error
this.continueLoad();
} else {
this.loginAndLoad();
}
});
}
Example of database call, this i called from this.continueLoad():
async getElements() {
const elements = this.db.list('settings/elements/da/');
const subscribe = await elements.snapshotChanges().subscribe(res => {
subscribe.unsubscribe();
let array = [];
res.forEach(child => {
let a: any;
a = child.payload.toJSON();
if (a.status == "published") {
array.push({key: child.key,title: a.title});
}
})
this.storage.set('elements', JSON.stringify(array));
})
}
This is the Realtime database rules:
{
"rules": {
".write": "!data.exists()",
"products": {
".read": "auth.uid != null",
".write": "auth.uid != null",
".indexOn": ["stats", "status", "user_warn", "image_approved", "image_added", "note", "description","title","date_published","date_edited","date_added"]
},
"searchwords": {
".read": "auth.uid != null",
".write": "auth.uid != null"
},
"users": {
".read": "auth.uid != null",
".write": "auth.uid != null && auth.provider != 'anonymous'"
},
"settings": {
".read": "auth.uid != null",
".write": "auth.uid != null && auth.provider != 'anonymous'"
},
"routes": {
".read": "auth.uid != null",
".write": "auth.uid != null && auth.provider != 'anonymous'"
},
"campaigns": {
".read": "auth.uid != null",
".write": "auth.uid != null && auth.provider != 'anonymous'"
}
}
}
Here is the Ionic info:
Ionic:
Ionic CLI : 6.16.1 (/usr/local/lib/node_modules/@ionic/cli)
Ionic Framework : @ionic/angular 5.6.7
@angular-devkit/build-angular : 0.1100.7
@angular-devkit/schematics : 11.0.7
@angular/cli : 11.0.7
@ionic/angular-toolkit : 3.1.1
Capacitor:
Capacitor CLI : 2.4.6
@capacitor/android : 2.4.7
@capacitor/core : 2.4.6
@capacitor/ios : 2.4.7
Utility:
cordova-res : 0.15.3
native-run : 1.3.0
System:
NodeJS : v14.17.0 (/usr/local/bin/node)
npm : 6.14.13
OS : macOS Big Sur
This is so frustrating... Hope there is some one with a logic idea of the problem :)
Upvotes: 1
Views: 393
Reputation: 169
Finally i found the problem. As mentioned in the beginning som plugins were updated. (After trying a new plugin). So after hours and hours of debugin. I downgraded to Firebase 8.1.2 and @angular/fire to 6.1.5 everything worked again..
Upvotes: 2