Reputation: 1180
i am try to build blog app using firebase database use angularfire2 , l want to allowed to users can read all data post by others users , but unsuccessfully l dont know where is problem .
database rules !
{
"rules": {
"report": {
"$uid": {
".read": "true",
".write": "$uid === auth.uid"
}
}
}
}
When l use this rules the users does not read all posts for other user ! , only l can read my post .
main code
export class FeedPage {
itemsRef: AngularFireList<any>;
items: Observable<any[]>;
constructor(public navCtrl: NavController, public navParams: NavParams, public fire: AngularFireAuth
,public alertCtrl: AlertController,public toastCtrl: ToastController,public popoverCtrl: PopoverController,
public db: AngularFireDatabase)
{
// // Use snapshotChanges().map() to store the key
// this.items = this.itemsRef.snapshotChanges().pipe(
// map(changes =>
// changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
// )
}
ionViewWillLoad(){
this.fire.authState.subscribe(data => {
if(data && data.email && data.uid){
this.toastCtrl.create({
message : ` welcome ${data.email}`,
duration:2000
}).present()
this.itemsRef = this.db.list(`report/`+data.uid);
// Use snapshotChanges().map() to store the key
this.items = this.itemsRef.snapshotChanges().pipe(
map(changes =>
changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
)
);
}
})
}
}
database
{
"report" : {
"8D3sENaBcLaXoGNnh1MPuoyj5LP2" : {
"-LWl294Hs6YjkvJE5pqi" : {
"post" : "ali",
"title" : "dd"
},
"-LWlEonKLWfOttzirqp7" : {
"post" : "sas",
"title" : "ass"
},
"-LWlGvn81Kes2A-1UcC2" : {
"post" : "asa",
"title" : "asass"
}
},
"WUM2HBkGo8TFDeOjEqO1s3lCj1p1" : {
"-LWlHlhyS9m3ECS3wIdk" : {
"post" : "qwqsasasa",
"title" : "as"
},
"-LWlHmXZAJdSPZurO7ii" : {
"post" : "qwqsasasa",
"title" : "as"
}
}
}
}
if l use this code to retrieve data l got empty html data !
this.itemsRef = this.db.list(`report`);
if l use this code to retrieve data l got only my own post not by others all users .
this.itemsRef = this.db.list(`report/${data.uid}`);
Upvotes: 3
Views: 1628
Reputation: 1660
As I said, I am not good with angularfire, but this line
this.itemsRef = this.db.list(`report/${data.uid}`);
To me seems to be pulling data for the user that is logging in only, I think it might need to be
this.itemsRef = this.db.list(`report`);
Then loop through all elements in the retrieved snapshot
UPDATE
Try moving the ".read": true,
above the $uid
, just under reports
,
copy the exact rule from the other answer
Upvotes: 2
Reputation: 598740
If you want to allow all users to read all posts by all users, you need to grant them access to the entire /report
node. In your security rules you do that, by move the ".read": true
rule up one level:
{
"rules": {
"report": {
".read": true,
"$uid": {
".write": "$uid === auth.uid"
}
}
}
}
But this means you'll also need to update your code to listen to all of /report
and then loop over the individual users under that.
Upvotes: 3