import Ember from 'ember';

export default Ember.Object.extend({
  firebase: Ember.inject.service(),

  open: function(options) {
    var provider = options.provider || options.authWith;

    return new Ember.RSVP.Promise((resolve, reject) => {
      if (!provider) {
        reject(new Error('`provider` must be supplied'));
      }

      if (provider === 'password') {
        if (!options.email && !options.password) {
          reject(new Error('`email` and `password` must be supplied'));
        }

        this.get('firebase').authWithPassword({
          email: options.email,
          password: options.password,
        }, (error, authData) => {

          if (error) {
            reject(error);
          } else {
            resolve(authData);
          }
        });

      } else if (provider === 'custom') {
        if (!options.token) {
          reject(new Error('A token must be supplied'));
        }
        this.get('firebase').authWithCustomToken({
          token: options.token
        }, (error, authData) => {
          if (error) {
            reject(error);
          } else {
            resolve(authData);
          }
        });
      } else if (provider === 'anonymous') {
        this.get('firebase').authAnonymously((error, authData) => {
          if (error) {
            reject(error);
          } else {
            resolve(authData);
          }
        });
      } else {
        this.get('firebase').authWithOAuthPopup(provider, (error, authData) => {
          if (error) {
            reject(error);
          } else {
            resolve(authData);
          }
        });
      }

    });
  }
});

import Ember from 'ember';

export default Ember.Object.extend({
  firebase: Ember.inject.service(),

  open: function(options) {
    var provider = options.provider || options.authWith;

    return new Ember.RSVP.Promise((resolve, reject) => {
      if (!provider) {
        reject(new Error('`provider` must be supplied'));
      }

      if (provider === 'password') {
        if (!options.email && !options.password) {
          reject(new Error('`email` and `password` must be supplied'));
        }

        this.get('firebase').authWithPassword({
          email: options.email,
          password: options.password,
        }, (error, authData) => {

          if (error) {
            reject(error);
          } else {
            resolve(authData);
          }
        });

      } else if (provider === 'custom') {
        if (!options.token) {
          reject(new Error('A token must be supplied'));
        }
        this.get('firebase').authWithCustomToken(options.token, (error, authData) => {
          if (error) {
            reject(error);
          } else {
            resolve(authData);
          }
        });
      } else if (provider === 'anonymous') {
        this.get('firebase').authAnonymously((error, authData) => {
          if (error) {
            reject(error);
          } else {
            resolve(authData);
          }
        });
      } else {
        this.get('firebase').authWithOAuthPopup(provider, (error, authData) => {
          if (error) {
            reject(error);
          } else {
            resolve(authData);
          }
        });
      }

    });
  }
});

Fix custom token support (cherry picked from commit f940abe5b66ab6e8ebc860e0693b21dba3324a7d)