Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a user with multiple attributes in Firebase with Swift?

I am so new to Firebase (a couple of hours only) and still trying to absorb most of its concepts. I followed the documentation but still found no direct answer to my question. I have a registration form in my app...it takes username, name, mobile, etc. I used the firebase createUser function but that only creates a user with email and password.

How can I create a user with multiple attributes (name, mobile, username, etc)? Later on, I would like to query a username and get all associated attributes with them so to do that I need to have the attributes.

P.S. coming from Parse, this is not as straightforward.

like image 560
ksa_coder Avatar asked Sep 14 '25 02:09

ksa_coder


2 Answers

Traditionally, you would create a /users node in Firebase that contains any other info you want to store about that user.

Use the uid (user_id) as the node name for each user.

users
  uid_0
   name: "Bill"
   food: "Pizza"
  uid_1
   name: "Ted"
   food: "Tacos"

This question has been asked before so please try searching for questions similar to yours before asking.

A bit more info here Firebase Users Node

like image 145
Jay Avatar answered Sep 16 '25 18:09

Jay


I am assuming that since you will be having multiple users, you will have them authenticated before they access your app. You may first create a Struct file in Swift, as follows:

import Foundation
import Firebase

struct User {
let uid: String
let email: String
let usermobilenumber : String
let userFirstName: String
let userLastName : String

// Initialize from Firebase
init(authData: FAuthData) {
    uid = authData.uid
    email = authData.providerData["email"] as! String
    userhandle = authData.providerData["userhandle"] as! String
    userFirstName = authData.providerData["userFirstName"] as! String
    userLastName = authData.providerData["userLastName"] as! String
}

// Initialize from arbitrary data
init(uid: String, email: String, userLastName: String, userFirstName: String, usermobilenumber: String) {
    self.uid = uid
    self.email = email
    self.userLastName = userLastName
    self.userFirstName = userFirstName
    self.usermobilenumber = userhandle

}
}

Using textfields, perform the user input task. Then, you need to access the Firebase node where you want to write the data (which you may have previously saved as ref = Firebase(url: "my-awesome-app.firebaseio.com/userinfo"), and simply ref.updateChildValues().

My answer is a schematic response to help you get started, because Firebase documentation is quite exhaustive and has answers to most of the questions that a beginner encounters. You should read this website, and go through the API References to help you completely achieve what you want.

like image 26
KuboAndTwoStrings Avatar answered Sep 16 '25 18:09

KuboAndTwoStrings