Vikas Singhal
Vikas Singhal

Reputation: 836

Adding uid to firebase database using angularfire

I am using angularfire for my current project. I am having hard time configuring security rules.

Here is my data structure:

{
   users : {
      uid1 : { .. },
      uid2 : { .. }
   }
}

Security rules:

{ 
  "rules" : { 
     "users" : {
        "$uid" : {
           ".write": "auth !== null && auth.uid === $uid",
           ".read": "auth !== null && auth.uid === $uid"
         }
     }
  }
}

I am using anonymous auth and "uid1" and "uid2" are nothing by auth.uid.

I am using following code to create

var obj = $firebaseObject(ref.child('users').child(auth.uid));

Its returning alright with $loaded but its not creating a uid node under users.

Upvotes: 0

Views: 468

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600131

Calling $firebaseObject creates a two-way binding: anything that changes to the object in the database, will automatically be synchronized to your application.

Calling $firebaseObject does however not create an object automatically.

To create a user, you can either use the Firebase JavaScript SDK:

ref.child('users').child(auth.uid).set({ name: 'Vikas Singhal' })

Or you can create it through AngularFire with:

var obj = $firebaseObject(ref.child('users').child(auth.uid));
obj.name = 'Vikas Singhal';
obj.$save();

Since AngularFire is built on top of the Firebase JavaScript SDK, mixing the two APIs works fine.

Upvotes: 2

Related Questions