在Firebase中查询用户以检查注册过程中是否存在用户名 [英] Query users in Firebase to check if username exists during sign up process

查看:63
本文介绍了在Firebase中查询用户以检查注册过程中是否存在用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

如何在用户注册过程中检查用户名是否已经存在?

How do I check if a username already exists during the user sign up process?

我不知道如何查询Firebase以确定是否存在特定值.

背景

Firebase结构:

Firebase structure:

Users
    0BBfrF1vVBXXxNxeVMes9MFkYNJ3
    name: "SAM"

    0oU9sf7CZxaDBx03t87lqTrv9UM2
    name: "JACK"

    IsXEqXov0obuwl1WOrHhCbfdfEo1
    name: "JEREMY"

在下面的代码中:

  • 我尝试检查 usernameField 中是否存在以下值: users \ userID \ name
  • child(userID)不检索任何内容
  • I attempt to check if value usernameField exists in: users\userID\name
  • child(userID) doesn't retrieve anything

_

let username = self.usernameField.text

        let userRef = ref.child("Users").child(userID).child("name")

        userRef.observeEventType(.Value, withBlock: { snapshot in

            if snapshot.value!.isEqual(username) {

                print("snapshot exists")

            } else {

                print("snapshot doesnt exist")

            }


            userRef.removeAllObservers()

            }, withCancelBlock: { error in

                print(error)

        })

推荐答案

执行此操作非常简单.

由于您是在第一次回调之后立即使用 removeAllObservers ,所以我假设您可以使用 observeSingleEventOfType 进行查看,而以后无需再转动任何观察者

Since you are using removeAllObservers right after the the first callback I'm assuming that you might take a look on using observeSingleEventOfType and you wont need to turn any observer later.

let username = self.usernameField.text
ref.child("Users").queryOrderedByChild("name").queryEqualToValue(username).observeSingleEventOfType(.Value, withBlock: { (snapshot) in 
    if snapshot.exists == true {
        print("snapshot exists")
    } else {
        print("snapshot doesnt exist")
    }
}) { (error) in
   print(error.localizedDescription)
} 

您还应该编写一些数据库规则,以确保服务器端的数据一致性和性能.从当前结构来看,由于您没有 username 作为 Users 分支的键,因此您不会很直截了当.

You should also write some database rules to guarantee the data consistency and performance in the server side. From the current structure you have this wont be straight-forward since you don't have the username as the key for your Users branch.

所以我可以看到两种可能的解决方案:

So I can see two possible solutions:

username 保存为/Users 键,您将只有一条规则来强制执行此键的唯一性.

Saving the username as the /Users key you will just have a rule to enforce this key uniqueness.

{ "rules": { 
  "Users": {
    ".indexOn":
    ".write": "true",
    "$username": {
        ".validate": "!root.child('Users').hasChild($username)"
    }
  }
}}

这需要对您的应用程序代码进行一些更改,以查看是否已经存在

This would need some changes on your application code to see if this already exists

ref.child("Users").child(username).observeSingleEventOfType(.Value, withBlock: { (snapshot) in 
        if snapshot.exists == true {
            print("snapshot exists")
        } else {
            print("snapshot doesnt exist")
        }
    }) { (error) in
       print(error.localizedDescription)
    } 

创建一个新分支来处理用户名唯一性

您想要保持与今天相同的结构,则需要进行其他一些更改.就像弗兰克(Frank)链接的问题一样,您将仅需要一个额外的分支来存储所有采用的用户名.因此,每当保存新用户时,您都需要先将用户名存储在另一个分支中.

Create a new branch to handle the username uniqueness

You want to keep the same structure you have today you will need to do some other changes. Just like the question that Frank linked you will need an extra branch only to store all the taken usernames. So whenever saving a new user you will need to first store the username in this other branch.

{ "rules": { 
  "Users": {
    ".indexOn": "name",
    ".write": "true",
    "$id": {
        ".validate": "!root.child('already_taken_names').hasChild(newData.child('name').val())"
    }
  },
  "already_taken_names": {
    "$username": {
        ".validate": "!root.child('Users').hasChild($username)"
    }
  }
}}

这篇关于在Firebase中查询用户以检查注册过程中是否存在用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆