1

We're trying to authenticate with a customToken to Firebase - this has been working before we moved to EmberCLI as we initiated these Firebase specific adapters at a later stage in the app runtime.

Now trying to initiate the authCustomToken at an earlier stage in the adapter with the flow as follows:

  • Initiate adapter
  • Hook on "init" and request a token from backend
  • When token received - authWithCustomToken

Code looks pretty much like this:

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

How is the best way of approaching this?

hussfelt
  • 251
  • 3
  • 12
  • So, everything is working? – givanse Nov 25 '14 at 16:31
  • No, not since I refactored to the above. In the Firebase documentation it says that you only need to authenticate one instance of "Firebase" and all of them will be authenticated. In the above case it seems that the adapter is initiated without authentication - then authenticating. This seems to result in all items loaded from the "carts" are non-authenticated and any attempt to modify will fail. What we most probably need is to authenticate the first firebase instance, but I can't seem to do that either as the firebase-property is required when creating the adapter. – hussfelt Nov 26 '14 at 06:48
  • 1
    I haven't used Firebase before and maybe I don't fully grasp the problem. However, it sounds like the type of problem where you need to setup X before anything else can be done. Have you looked at creating initializers? http://emberjs.com/guides/understanding-ember/dependency-injection-and-service-lookup/#toc_dependency-injection-with-code-register-inject-code – givanse Nov 26 '14 at 06:56
  • Of course!! That might work. Trying this now. – hussfelt Nov 26 '14 at 06:58
  • If I don't pass `firebase: new Firebase('https://firebasehost.com')` when initiating the adapter I get an error saying: Uncaught Error: Please set the `firebase` property on the adapter. Tried injecting the firebase instance from an initializer (works well), but how would I assign the firebase property this value to begin with? – hussfelt Nov 26 '14 at 07:16

1 Answers1

0

The error is that it seems EmberFire as of yet does not support a validation child rule. Most probably because it's trying to update the cart before the item is inserted.

This was working in Ember 1.7 and a previous version of EmberFire.

The rule-block below that does work, with the part that did not work uncommented:

"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)"
                    }
                }*/
            }
        }
    }
}
hussfelt
  • 251
  • 3
  • 12