[英]How to check unique username in Firebase Database (swift)
我有Firebase數據庫,其中每個用戶都有自己的電子郵件和用戶名。 如何檢查唯一的用戶名? 我試圖這樣做,但是我的代碼無法正常工作,因此不同的用戶可以使用相同的用戶名
usernameField.isHidden = false
let username:String = self.usernameField.text!
if (usernameField.text?.isEmpty == false){
ref.child("users").queryOrdered(byChild("username").queryEqual(toValue: username).observeSingleEvent(of: .value, with: { snapshot in
if snapshot.exists(){
print("username exist")
}else{
ref.root.child("users").child(userID).updateChildValues(["username": username])
}
})
}
我在Firebase中有點新手,我為每個用戶存儲電子郵件和用戶名,例如newUserReference.setValue(["username":String(), "email" : self.emailTextField.text!])
。 在下一個視圖上,用戶可以在usernameField.text
鍵入username
,並且該值將添加到Firebase數據庫中。 但是,如果下一個用戶(用戶2)將輸入與上一個用戶相同的用戶名,則必須將其阻止,因為用戶名應唯一
如果您嘗試在登錄時存儲用戶的ID,請在收到成功的登錄響應后執行以下操作:
創建一個共享實例來存儲ID
class userDataSource {
var id : String? // variable to store your users ID
static let sharedInstance = PageDataSource() // global access to this dataSource
private init() {}
}
成功登錄后為id分配一個值
func getIDFromLogin() {
if let user = Auth.auth().currentUser {
print(user.uid)
userDataSource.sharedInstance.id = user.uid
}
}
然后,您可以執行以下操作以查看每個ID:
ref.child("users").observeSingleEvent(of: .value, with: { snapshot in
if let snapshots = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshots {
print(snap.key) // you can compare userDataSource.sharedInstance.id to this value
}
}
})
或者,如果您只想要該用戶的數據,請執行以下操作:
ref.child("users").child(userDataSource.sharedInstance.id!).observeSingleEvent(of: .value, with: { snapshot in
if let snapshots = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshots {
print(snap)
}
}
})
編輯以更准確地回答您的問題
這是一個更符合您問題的答案。 我將建議的第一件事是為您向Firebase添加一個表,該表僅包含用戶名以及它們所屬的.uid
。 您將需要先通讀該表以確保沒有其他人擁有該用戶名,然后相應地更新該表:
// This function will check through all of the usernames and return a true or false value in the completion handler
func checkUsernames(_ completion: @escaping(_ success: Bool) -> Void) {
ref.child("usernames").observeSingleEvent(of: .value, with: { snapshot in
if let snapshots = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshots {
if snap.value == username {
completion(false)
}
}
completion(true)
} else {
completion(true) // TODO: check for errors before setting completion
}
})
}
// this function will set the username values in Firebase
func storeUsername() {
let usernameRef = ref.child("usernames")
usernameRef.updateChildValues(["\(userDataSource.sharedInstance.id!)" : username])
}
}
}
假設您已經處理了username
變量並設置了它的值,則將調用以下函數:
checkUsernames({ (success) in
if success {
storeUsername()
// you may also want to update your "users" table here as well
} else { print("Duplicate Username") } // handle alert or something here
})
您仍然需要使用queryOrdered(byChild:)
指示要排序/過濾的屬性:
if (usernameField.text?.isEmpty == false){
ref.child("users").queryOrdered(byChild:"username").queryEqual(toValue: username).observeSingleEvent(of: .value, with: { snapshot in
if snapshot.exists(){
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.