在Firebase中通过电子邮件查找UID [英] Finding an UID by email in Firebase

查看:57
本文介绍了在Firebase中通过电子邮件查找UID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先尝试实例化数据库中的每个用户,如下所示:

I first attempted to instantiate each user in the database like so:

emails : {
    email: "jerry@gmail.com" {
        uid: "x"
    }
}

我很快发现我无法存储电子邮件,因为它具有 @ ..我本来打算让用户看起来像这样:

I quickly found out that I can't store an email because it has an @ and .. I was originally going to do an user look up like so:

func userLookUpByEmail (email: String) -> String {
    var userID: String = "nil"

    ref.queryOrderedByChild("emails").queryEqualToValue(email).observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in
            if snapshot.value != nil {
                print(snapshot.value)
                userID = snapshot.value as! String
            }
            else {
                print ("user not found")
                userID = "nil"
            }
    })
    return userID
}

但是,我意识到这是行不通的.通过提供电子邮件来接收UID的有效方法是什么?

However, I'm realizing that won't work. What is an effective way to receive an UID from providing an email?

推荐答案

回答您的问题,因为您不能将电子邮件作为子键,所以可以执行另一种方法,将uid设置为键,然后作为孩子的电子邮件.

Answering your question, since you can't have the email as the child key you can do the other way, setting the uid as the key and the email as the child.

emails : {
    uid: {
        email: "jerry@gmail.com"
    }
}

然后您的代码将有所更改.

And then your code will change a little bit.

func userLookUpByEmail (email: String, completionHandler: (result: String) -> Void) {
    var userID: String = "nil"
    ref.child("emails").queryOrderedByChild("email").queryEqualToValue(email).observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in
                if snapshot.value != nil {
                    print(snapshot.key)
                    userID = snapshot.key as! String
                }
                else {
                    print ("user not found")
                    userID = "nil"
                }
                completionHandler(userID)
        })
 }

呼叫 userLookUpByEmail :

userLookUpByEmail(email) {
    (result: String) in
    print("\(result)")
}

另一方面,我想知道为什么您的数据库中有一个 emails 分支,因为您可能有一个 users 分支(如果您还没有的话))并在其中存储所有用户数据,包括电子邮件.这将使您的数据结构更加干净和可靠.再次,我在想知道,因为我不完全了解您的需求.

On the other hand I'm wondering why do you have an emails branch in your database since you could have an users branch (if you don't have yet) and store any user data there, including the email. This would make your data structure much more clean and reliable. Again, I'm wondering here since I don't exactly know your needs.

users : {
    uid: {
        email: "jerry@gmail.com",
        name: "Jerry Maguire",
        ...
    }
}

这篇关于在Firebase中通过电子邮件查找UID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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