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.
If i serve the app in chrome. And i serve my firebase hosted backend-app in another tab. The app starts working…
I’ve made a completely new projekt and copied the src folder, added all plugins from npm updated to capacitor 3, still the same problem…
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.1102.13
@angular-devkit/schematics : 11.2.13
@angular/cli : 11.2.13
@ionic/angular-toolkit : 3.1.1
Capacitor:
Capacitor CLI : 3.0.0
@capacitor/android : not installed
@capacitor/core : 3.0.0
@capacitor/ios : 3.0.0
Utility:
cordova-res : 0.15.3
native-run : 1.3.0
System:
NodeJS : v12.16.2 (/usr/local/bin/node)
npm : 6.14.4
OS : macOS Big Sur
This is so frustrating… Hope there is some one with a logic idea of the problem i’ve asked the question on StackOverflow, but without help…