FirebaseExtended / emberfire

The officially supported adapter for using Firebase with Ember
https://firebaseopensource.com/projects/firebaseextended/emberfire/
MIT License
684 stars 263 forks source link

adapter.fetch is not a function when using TORII for Auth in 3.0.0-rc6 #628

Closed mohitsud closed 4 years ago

mohitsud commented 4 years ago
e.g.
DEBUG: -------------------------------
DEBUG: Ember      :  3.0.0
DEBUG: Ember Data : 3.0.0
DEBUG: Firebase   : 7.x
DEBUG: EmberFire  : 3.0.0-rc6
DEBUG: -------------------------------

Steps to reproduce

  1. I just created a new ember project
  2. I installed emberfire@next, and torii
  3. I followed the instructions here: https://github.com/firebase/emberfire/blob/master/docs/guide/authentication.md. In torii, the beforeModel has a call to this.get('session').fetch
  4. Attempt to log in.
  5. I receive an error "adapter.fetch is not a function"


When I trace through the commit history for ember fire, I notice in version 2.0.10 there is a fetch function in the torii-adapter, however this is missing in emberfire 3.0.0-rc6.

**Version 2.0.10:**
https://github.com/firebase/emberfire/blob/v2.0.10/addon/torii-adapters/firebase.js

**Version 3.x**
https://github.com/firebase/emberfire/blob/master/addon/torii-adapters/firebase.ts
Is anyone able to explain what to do now that FETCH is no longer available?

I would welcome thoughts on why FETCH was removed and how we can go about resolving this issue.
mohitsud commented 4 years ago

I basically took code from 2.0.10 and added it to torii-adapters/firebase.js to get it to work on 3.0.0-rc6

import ToriiFirebaseAdapter from 'emberfire/torii-adapters/firebase';
import Ember from 'ember';

export default ToriiFirebaseAdapter.extend({

    /**
   * Restore existing authenticated session
   *
   * @return {Promise}
   */
  fetch() {
    return this.fetchAuthState_()
      .then((user) => {
        if (!user) {
          return this.fetchRedirectState_();
        }
        return user;
      })
      .then((user) => {
        if (!user) {
          return Ember.RSVP.reject(new Error('No session available'));
        }
        return this.open(user);
      })
      .catch((err) => Ember.RSVP.reject(err));
  },

  /**
   * Fetches the redirect user, if any.
   *
   * @return {!Promise<?firebase.User>}
   * @private
   */
  async fetchRedirectState_() {
    let auth = await this.get('firebaseApp').auth();
    return auth.getRedirectResult()
      .then(result => result.user);
  },

  /**
   * Promisifies the first value of onAuthStateChanged
   *
   * @return {!Promise<?firebase.User>}
   * @private
   */
  async fetchAuthState_() {
    return new Ember.RSVP.Promise(async (resolve, reject) => {
      let auth = await this.get('firebaseApp').auth();
      const unsub = auth.onAuthStateChanged((user) => {
        unsub();
        resolve(user);
      },
      (err) => {
        unsub();
        reject(err);
      });
    });
  },

});