使用适配器在customToken中进行身份验证的位置

时间:2014-11-25 16:18:14

标签: ember.js ember-data emberfire

我们正在尝试使用customToken对Firebase进行身份验证 - 在我们迁移到EmberCLI之前,我们已经开始工作了,因为我们在应用运行时的稍后阶段启动了这些Firebase特定适配器。

现在尝试使用以下流程在适配器的早期阶段启动authCustomToken:

  • 启动适配器
  • 挂钩“init”并从后端请求令牌
  • 收到令牌时 - authWithCustomToken

代码看起来非常像这样:

import DS from 'ember-data';

/**
 * CartAdapter
 * @class adapters.Cart
 * @extends DS.FirebaseAdapter
 */
export default DS.FirebaseAdapter.extend(ajax, {
    firebase: new Firebase('https://firebasehost.com'),
    pathForType: function() {
        return 'carts/' + this.get('sessionService').get('userId');
    },
    initAdapter: function() {
        this.ajaxRequest('backendhost/firebase/').then(function(data) {
            var ref = new Firebase('https://firebasehost.com');
            ref.authWithCustomToken(data.token);
        });
    }.on('init')
});

如何最好地接近这个?

1 个答案:

答案 0 :(得分:0)

错误是EmberFire似乎尚不支持验证子规则。很可能是因为它在插入项目之前尝试更新购物车。

这适用于Ember 1.7和之前版本的EmberFire。

下面的规则块确实有效,部分无法解除注释:

"rules": {
    // All data is accessible
    ".read": true,
    ".write": true,
    "cartItem": {
        "$userid": {
            // A list of users carts.
            "$cartitemid": {
                // Only the user can read and write their own entries into this list.
                ".write": "auth != null && $userid ==auth.uid",
                ".read": "auth != null && $userid ==auth.uid",
                "cart": {
                    // The following relation should only contain an actual id from the "cart" list.
                    "$cartid": {
                        ".validate": "root.child('carts').hasChild($cartid)"
                    }
                }
            }
        }
    },
    "carts": {
        "$userid": {
            "$cartid": {
                // The user is allowed to read and write everything in their cart.
                ".read": "auth != null && $userid ==auth.uid",
                ".write": "auth != null && $userid ==auth.uid"
                /*"items": {
                    // The following list should only contain actual ids from the "cartItems" list.
                    "$itemid": {
                        ".validate": "root.child('cartItem/' + $userid).hasChild($itemid)"
                    }
                }*/
            }
        }
    }
}