我将如何使用Firebase创build用户名?
我还是新手,用Xcode和Firebase编码。 我已经能够创build一个注册和login页面与Firebase身份validation。 我一直在想如何让人们创build自定义的唯一用户名。 我一直在网上search一段时间,我找不到多less。
我想了解如何在我的应用程序中实现用户名,以便用户可以创build唯一的用户名并拥有自己的configuration文件。
除了我的评论。
let currentUserId = FIRAuth.auth()?.currentUser?.uid let currentUserRef = FIRDatabase.database().reference(withPath: "MainDataBase/users").child(currentUserId!) // MainDataBase/users - is part from you database currentUserRef.observeSingleEvent(of: .value, with: { snapshot in self.userInfo = UserItem(snapshot: snapshot) self.label.text = "Welcome " + (self.userInfo.login)! // for example it will show login })
祝你好运
编辑:最好有这样的事情:
var userInfo: UserItem! { didSet { self.label.text = "Welcome " + (self.userInfo.login)! // for example it will show login // other actions }
在Firebase Auth中创build用户之后,请执行以下操作:
let newUser = UserItem(uid: < Value >, email: < Value >, login: < Value >, createdDate: < Value >) // Create a child path with a key set to the uid underneath the "users" node let refToMainDB = FIRDatabase.database().reference(withPath: "< PathToMainDB >") refToMainDB.child("users").child(< uid >).setValue(newUser.toAnyObject()
例如我的UserItem结构:
import Foundation import FirebaseAuth import FirebaseDatabase struct UserItem { let uid: String let email: String let login: String let bioDescription: String let nameAndSename: String let createdDate: String let ref: FIRDatabaseReference? init(uid: String, email: String, login: String, bioDescription: String = "", nameAndSename: String = "", createdDate: String) { self.uid = uid self.email = email self.login = login self.bioDescription = bioDescription self.nameAndSename = nameAndSename self.createdDate = createdDate self.ref = nil } init(snapshot: FIRDataSnapshot) { uid = snapshot.key let snapshotValue = snapshot.value as! [String: AnyObject] email = snapshotValue["email"] as! String login = snapshotValue["login"] as! String bioDescription = snapshotValue["bioDescription"] as! String nameAndSename = snapshotValue["nameAndSename"] as! String createdDate = snapshotValue["createdDate"] as! String ref = snapshot.ref } func toAnyObject() -> Any { return [ "uid": uid, "email": email, "login": login, "bioDescription": bioDescription, "nameAndSename": nameAndSename, "createdDate": createdDate ] } }
你可以在这里查看我的代码示例希望它有帮助