Where to authenticate with customToken using adapters

97 views Asked by At

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?

1

There are 1 answers

0
hussfelt On BEST ANSWER

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)"
                    }
                }*/
            }
        }
    }
}