在 Firebase 中设置用户自己的密钥 [英] Setting users' own key in Firebase

查看:34
本文介绍了在 Firebase 中设置用户自己的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题已经这里,但由于 AngularFire 更新了他们的 API,提供的解决方案不再可用.

The question is asked already here, but since AngularFire updates their API, the provided solution is not usable anymore.

我正在做的是使用电子邮件/密码创建新用户的帐户,将该用户存储在 Firebase 身份验证服务中,并将额外信息存储在数据库中.

What I am doing is to create new user's account with email/password, store that user in Firebase Authentication service, and store extra information in the database.

然而,在认证中,用户的密钥为simplelogin:29,而在数据中,每个用户都有自己的散列密钥,如-JpwB9oUVkeT_XRdiX2V.我确实需要他们的钥匙是一样的.

However, in the Authentication, user has their key as simplelogin:29, and in the Data, each user has their hashed key, as -JpwB9oUVkeT_XRdiX2V. I do need their keys to be the same.

下面是创建新用户的代码,使用 auth.$createUser 注册并在用户模型中存储额外信息.

Below is the code to create new user, registered with auth.$createUser and store extra information in the user model.

var ref = new Firebase('https://myapp.firebaseio.com/');
var auth = $firebaseAuth(ref);

auth.$createUser({
    email: user.email,
    password: user.password 
    }).then(function(regUser) {

    var ref = new Firebase('https://myapp.firebaseio.com/users/')
    var firebaseUser = $firebaseArray(ref);

    var userInfo = {
        key         : regUser.uid, // ex: simplelogin:29
        date        : Firebase.ServerValue.TIMESTAMP,
        firstname   : user.firstname,
        lastname    : user.lastname,
        email       : user.email,
    }; // user info

    firebaseUser.$add(userInfo);
});

它可以工作,但不是应该的方式,因为用户在身份验证和数据中的密钥仍然不同.

It works, but not the way supposed to be, as a user's key in Authentication and Data are still different.

推荐答案

当您存储用户数据时,您通常希望键入 uid.毕竟 uid 已经是唯一的,使用 is 作为键可以确保您永远不会有两个节点用于相同的 uid.

When you're storing user data, you typically want to key on uid. After all the uid is already unique and using is as the key ensures that you'll never end up with two nodes for the same uid.

当您调用 $add 时,它会为您存储的对象生成一个新的所谓推送 ID.当您有数据列表时,这很好,但在这种情况下,您的数据结构更像是映射:您将 uid 映射到用户信息.

When you call $add it generates a new so-called push ID for the object that you're storing. This is great when you have lists of data, but in this case your data structure is more map like: you map uid to the user info.

所以解决方案是不调用$add,而是:

So the solution is to not call $add, but instead:

var ref = new Firebase('https://myapp.firebaseio.com/');
var auth = $firebaseAuth(ref);

auth.$createUser({
    email: user.email,
    password: user.password 
}).then(function(regUser) {

    var ref = new Firebase('https://myapp.firebaseio.com/users/')

    var userInfo = {
        key         : regUser.uid, // ex: simplelogin:29
        date        : Firebase.ServerValue.TIMESTAMP,
        firstname   : user.firstname,
        lastname    : user.lastname,
        email       : user.email,
    }; // user info

    ref.child(regUser.uid).set(userInfo);
});

请注意,这仅使用常规的 Firebase JavaScript SDK,它将与您可能已经拥有的数据的任何 AngularFire 侦听器.

Note that this just uses the regular Firebase JavaScript SDK, which will interact totally fine with any AngularFire listeners you may already have on the data.

这篇关于在 Firebase 中设置用户自己的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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