问题
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