4

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Most Firebase developers end up with a `/users/` list in their Database. This allows you to store whatever information you want *and* it allows you to list the information across users (for which Authentication currently doesn't have an API). See for example this: http://stackoverflow.com/questions/36224004/how-to-save-users-name-along-with-email-in-firebase/36224452#36224452 – Frank van Puffelen Jul 26 '16 at 21:53

1 Answers1

1

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!

Rob Winters
  • 151
  • 1
  • 10