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

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

问题描述

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



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



然而,在认证中,用户的密钥为 simplelogin:29 ,在Data中,每个用户都有他们的哈希键,如 -JpwB9oUVkeT_XRdiX2V 。我确实需要他们的钥匙是一样的。



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

  var ref = new Firebase('https://myapp.firebaseio.com/'); 
var auth = $ firebaseAuth(ref);
$ b 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,
}; //用户信息

firebaseUser。$ add(userInfo);
});

它可以工作,但不是应有的方式,因为用户在身份验证和数据不同。 当你存储用户数据时,你通常需要键入 uid $ C>。在所有 uid 已经是唯一的,并且使用作为关键字的时候,确保你永远不会为同一个 uid $ add 时,它会为对象生成一个新的所谓的推送ID你正在存储。当你有数据列表的时候,这是非常好的,但是在这种情况下,你的数据结构更像地图: b

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

  var ref = new Firebase('https://myapp.firebaseio.com/'); 
var auth = $ firebaseAuth(ref);
$ b 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,//例如:simplelogin:29
date:Firebase.ServerValue.TIMESTAMP,
firstname:user.firstname,
lastname:user.lastname,
email:user.email,
}; //用户信息

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

请注意,这只是使用普通的 Firebase JavaScript SDK ,它将与任何您已经拥有的数据的AngularFire监听器进行交互。


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

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.

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.

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.

解决方案

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.

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.

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);
});

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天全站免登陆