user5091780
user5091780

Reputation:

How to to add extra user information to Firebase

I'm currently building an iOS app that has its own database on Firebase to handle the app's basic functionality. However I want to add more information to each user (aside from the uid, email and password) so I can validate some steps in my app. What's the best way to achieve this, hierarchy-wise? I'm using the new Firebase btw.

Upvotes: 4

Views: 2500

Answers (1)

Rob Winters
Rob Winters

Reputation: 161

There really isn't a schema. You write the values into a heirarcy you want. First you reference the UID.

In Swift it would be:

var usersRef = ref.childByAppendingPath("users")

Then you would create an object with all values you want to write. You could also write the values directly without making an object first.

        let newUser = [
        "provider": authData.provider,
        "displayName": authData.providerData["displayName"] as? NSString as? String
    ]

Then write the values with:

        ref.childByAppendingPath("users")
       .childByAppendingPath(authData.uid).setValue(newUser)

The docs are tricky to follow. The reference for this example is https://www.firebase.com/docs/ios/guide/user-auth.html

This block of code will give you:

{
"users": {
"6d914336-d254-4fdb-8520-68b740e047e4": {
  "displayName": "alanisawesome",
  "provider": "password"
},
"002a448c-30c0-4b87-a16b-f70dfebe3386": {
  "displayName": "gracehop",
  "provider": "password"
  }
}
}

Hope this helps!

Upvotes: 1

Related Questions