5

I'm relatively new to Firebase and trying to develop an app which requires users to give their name and surname in sign up page. But firebase createUserWithEmail method only stores email and password. How can I add this name and surname data to my app and link them with the users? I suppose i need to use FIRStorage but in that way, only options to saving information is saving either from NSData or NSURL, how can I save my name and surname strings?

Thanks in advance!

Edit1: I suppose i was wrong, I need to use real time database rather than storage to save user data. But still I need help. I am coming from Parse and this seems complicated.

adolfosrs
  • 9,286
  • 5
  • 39
  • 67
Atakan Cavuslu
  • 914
  • 1
  • 9
  • 17
  • Use FIRDstabase and then just for user x there is a name key that has a value of Bob. – Cody Weaver Jun 20 '16 at 16:38
  • You would typically store additional user data in a /users node. These are the old docs but they explain it much better than the new ones. [User Authentication](https://www.firebase.com/docs/ios/guide/user-auth.html) – Jay Jun 20 '16 at 17:42

1 Answers1

5

Yes, you need to use the realtime database to store your user data. So after creating it you can call the setValue().

FIRAuth.auth()!.createUserWithEmail(email, password: pwd, completion: { authData, error  in
    if error == nil {    
        let userData = ["name": name,
                        "surname ": surname]
        let ref = FIRDatabase.database().reference()
        ref.child('users').child(authData!.uid).setValue(userData)
    } else {
        // handle error
    }
})
adolfosrs
  • 9,286
  • 5
  • 39
  • 67
  • 2
    Be careful with this: creating a new user also authenticates them. The user that created the new user is also un-authenticated. This means if you plan on having a user that can create other users, it isn't going to happen in v3.x until Firebase fixes it. – Jay Jun 20 '16 at 17:45
  • @Jay you are right. my bad on this, just editted. Thanks for pointing it out. – adolfosrs Jun 20 '16 at 17:58
  • Thanks for replies :) I have one more question about this 'ref' variable. How do i create this variable and where should I create it. I looked up documents but there is not much good info about it, sadly. – Atakan Cavuslu Jun 20 '16 at 20:43
  • @AtakanCavuslu sorry for not adding it. Just edited the code with the proper info. – adolfosrs Jun 20 '16 at 20:46
  • Thank you very much for quick reply, but when i add 'setValue(userData)' it gives error: cannot convert value type [string:string] to 'AnyObject?'. What should I do? – Atakan Cavuslu Jun 20 '16 at 20:49
  • @adolfosrs I did tried this method, but it does not save name and surname. How should i create /users node in realtime database? can you clarify and help me please, thank you – Atakan Cavuslu Jun 20 '16 at 21:34
  • @AtakanCavuslu Sorry for the late reply. Are you still facing the error? did you tried hardcoding the name/surname? `let userData = ["name": "Atakan", "surname ": "Cavuslu"]`? – adolfosrs Jun 20 '16 at 21:42
  • ref.child('users').child(authData!.uid).setValue(userData) - there is an error in this line actually, it says "cannot convert value of type [string:string?] to expected argument type 'AnyObject?'. What should I do here? Also should I do any changes in the database tab of Firebase or does it automaticly create /users node? – Atakan Cavuslu Jun 20 '16 at 21:47
  • Okay, i figured it out. I just needed to force unwrap the fields. Since there was no error when I hardocing the name surname. Thank you very much – Atakan Cavuslu Jun 20 '16 at 21:53
  • @AtakanCavuslu how are you declaring the name/surname you are passing on the object? Try going with `let userData = ["name": name, "surname ": surname] as AnyObject` – adolfosrs Jun 20 '16 at 22:00
  • @AtakanCavuslu Great to here it from you. Let me know if there is any other information I can provide. Otherwise could you accept the answer? Thanks – adolfosrs Jun 20 '16 at 22:07