Firebase 查询唯一用户名 swift
Firebase querying for unique Username swift
我搜索了这个问题,但其中 none 个答案对我有用。
我想这样做,当用户注册一个帐户时,它会在创建帐户之前检查他们输入的用户名是否已经存在。我试过在 firebase 中使用查询,但我似乎无法让它工作。
这是我的 firebase 数据的图像:My firebase data tree
我如何使用查询来查找键 "username" 的字符串?
我是这样做的:
var text = "Your username"
let dbRef = FIRDatabase.database().reference().child("users")
dbRef.queryOrdered(byChild: "name").queryEqual(toValue: text).observeSingleEvent(of: .value, with: { snapshot in
if !snapshot.exists() {
// Name doesn't exist
}
if let data = snapshot.value as? [String: [String: String]] {
// it should exist if it reaches here
}
})
确保在您的数据库规则中为 "name" 上的 "users" 节点编制索引以优化性能。
你可以这样做,创建一个带有完成块的函数来检查用户名是否已经存在于你的 Firebase 数据库中,并根据它创建新用户
func checkUserNameAlreadyExist(newUserName: String, completion: @escaping(Bool) -> Void) {
let ref = FIRDatabase.database().reference()
ref.child("users").queryOrdered(byChild: "username").queryEqual(toValue: newUserName)
.observeSingleEvent(of: .value, with: {(snapshot: FIRDataSnapshot) in
if snapshot.exists() {
completion(true)
}
else {
completion(false)
}
})
}
现在创建新用户时只需调用此函数即可:
self.checkUserNameAlreadyExist(newUserName: "Johnson") { isExist in
if isExist {
print("Username exist")
}
else {
print("create new user")
}
}
我接下来这样做:
寄存器的作用:
@IBAction func signUpButtonTapped(_ sender: Any) {
User.getItemByLogin(for: userLogin.text!,
completion: { userItem in
if userItem == nil {
self.createAndLogin()
} else {
self.showAlertThatLoginAlreadyExists()
}
})
}
private func createAndLogin() {
FIRAuth.auth()!.createUser(withEmail: userEmail.text!,
password: userPassword.text!) { user, error in
if error == nil {
// log in
FIRAuth.auth()!.signIn(withEmail: self.userEmail.text!,
password: self.userPassword.text!,
completion: { result in
// create new user in database, not in FIRAuth
User.create(with: self.userLogin.text!)
self.performSegue(withIdentifier: "fromRegistrationToTabBar", sender: self)
})
} else {
print("\(String(describing: error?.localizedDescription))")
}
}
private func showAlertThatLoginAlreadyExists() {
let alert = UIAlertController(title: "Registration failed!",
message: "Login already exists.",
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
}
我的用户 class 功能。 API 就像 class:
static func getItemByLogin(for userLogin: String,
completion: @escaping (_ userItem: UserItem?) -> Void) {
refToUsersNode.observeSingleEvent(of: .value, with: { snapshot in
for user in snapshot.children {
let snapshotValue = (user as! FIRDataSnapshot).value as! [String: AnyObject]
let login = snapshotValue["login"] as! String // getting login of user
if login == userLogin {
let userItem = UserItem(snapshot: user as! FIRDataSnapshot)
completion(userItem)
return
}
}
completion(nil) // haven't founded user
})
}
按照您的方式,您应该将 login
与 username
交换。
希望对您有所帮助
我搜索了这个问题,但其中 none 个答案对我有用。
我想这样做,当用户注册一个帐户时,它会在创建帐户之前检查他们输入的用户名是否已经存在。我试过在 firebase 中使用查询,但我似乎无法让它工作。
这是我的 firebase 数据的图像:My firebase data tree
我如何使用查询来查找键 "username" 的字符串?
我是这样做的:
var text = "Your username"
let dbRef = FIRDatabase.database().reference().child("users")
dbRef.queryOrdered(byChild: "name").queryEqual(toValue: text).observeSingleEvent(of: .value, with: { snapshot in
if !snapshot.exists() {
// Name doesn't exist
}
if let data = snapshot.value as? [String: [String: String]] {
// it should exist if it reaches here
}
})
确保在您的数据库规则中为 "name" 上的 "users" 节点编制索引以优化性能。
你可以这样做,创建一个带有完成块的函数来检查用户名是否已经存在于你的 Firebase 数据库中,并根据它创建新用户
func checkUserNameAlreadyExist(newUserName: String, completion: @escaping(Bool) -> Void) {
let ref = FIRDatabase.database().reference()
ref.child("users").queryOrdered(byChild: "username").queryEqual(toValue: newUserName)
.observeSingleEvent(of: .value, with: {(snapshot: FIRDataSnapshot) in
if snapshot.exists() {
completion(true)
}
else {
completion(false)
}
})
}
现在创建新用户时只需调用此函数即可:
self.checkUserNameAlreadyExist(newUserName: "Johnson") { isExist in
if isExist {
print("Username exist")
}
else {
print("create new user")
}
}
我接下来这样做:
寄存器的作用:
@IBAction func signUpButtonTapped(_ sender: Any) {
User.getItemByLogin(for: userLogin.text!,
completion: { userItem in
if userItem == nil {
self.createAndLogin()
} else {
self.showAlertThatLoginAlreadyExists()
}
})
}
private func createAndLogin() {
FIRAuth.auth()!.createUser(withEmail: userEmail.text!,
password: userPassword.text!) { user, error in
if error == nil {
// log in
FIRAuth.auth()!.signIn(withEmail: self.userEmail.text!,
password: self.userPassword.text!,
completion: { result in
// create new user in database, not in FIRAuth
User.create(with: self.userLogin.text!)
self.performSegue(withIdentifier: "fromRegistrationToTabBar", sender: self)
})
} else {
print("\(String(describing: error?.localizedDescription))")
}
}
private func showAlertThatLoginAlreadyExists() {
let alert = UIAlertController(title: "Registration failed!",
message: "Login already exists.",
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
}
我的用户 class 功能。 API 就像 class:
static func getItemByLogin(for userLogin: String,
completion: @escaping (_ userItem: UserItem?) -> Void) {
refToUsersNode.observeSingleEvent(of: .value, with: { snapshot in
for user in snapshot.children {
let snapshotValue = (user as! FIRDataSnapshot).value as! [String: AnyObject]
let login = snapshotValue["login"] as! String // getting login of user
if login == userLogin {
let userItem = UserItem(snapshot: user as! FIRDataSnapshot)
completion(userItem)
return
}
}
completion(nil) // haven't founded user
})
}
按照您的方式,您应该将 login
与 username
交换。
希望对您有所帮助