How can i create a user with info about Display Name, username & Profile pic?

扶醉桌前 提交于 2019-12-19 12:00:11

问题


How can i create a user with info about Display Name, username & Profile pic?

Can I use this code?

if let user = FIRAuth.auth()?.currentUser
let name = user.displayName
let email = user.email
let photoUrl = user.photoURL
let uid = user.uid; // The user's ID, unique to the Firebase project.
                    // Do NOT use this value to authenticate with
                    // your backend server, if you have one. Use
                    // getTokenWithCompletion:completion: instead.
} else {
  // No user is signed in.
}

If not, for what purpose this code is used? And which code can be used to determine username, profile pic etc,?

I'm new to firebase.


回答1:


Firebase provides authentication for users using the FIRAuth Clases/API

However they can't be edited with custom fields like the Firebase Database.

To allow custom fields for user, you need to duplicate users in the Database with the same unique uid you receive in the auth of the user. Something like this

if let user = FIRAuth.auth()?.currentUser {
  let name = user.displayName
  let email = user.email
  let photoUrl = user.photoURL
  let uid = user.uid; 

  let ref = FIRDatabase.database().reference()
  let key = uid

        let userDict = [ "name"    : name ,
                         "email"   : email,
                     "photoUrl"    : photoUrl]

        let childUpdates = ["/users/\(key)": userDict]
        ref.updateChildValues(childUpdates, withCompletionBlock: { (error, ref) -> Void in
              // now users exist in the database
        })
} 

We push it at the users end point in our db and with the uid key by the following line /users/\(key).



来源:https://stackoverflow.com/questions/37649464/how-can-i-create-a-user-with-info-about-display-name-username-profile-pic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!