mardi 23 février 2016

Torii session redirects to login page on refresh

I am building a web app using Ember.js, Ember-cli, Torii & Emberfire.

The problem is every time I reload/refresh the page, it redirects me to the login page .. almost like logging me out, but apparently I am still signed in because I can still see protected pages. And only when I click the 'logout' button, I am restricted from protected pages. Since Torii does not persist my session for me, I had to look around and figure out how to do that, and now it doesn't log me out on refresh but redirects me to the login page with some other funny actions.

I asked another question here, which has most of my relevant code on it. (This is the version where it logs me out on refresh)

And this is the new code (where it doesn't log me out on refresh, but redirect me to login page):

My config/environment.js file looks like this:

firebase: 'http://ift.tt/1SXgd6J',
    torii: {
      sessionServiceName: 'session'
    },

app/adapters/application.js:

import Ember from 'ember';
import FirebaseAdapter from 'emberfire/adapters/firebase';

const { inject } = Ember;

export default FirebaseAdapter.extend({
  firebase: inject.service(),
});

app/torii-adapters/application.js:

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

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

  open(authData) {
    return this._findOrCreateUser(authData)
      .then((user) => {
        return { currentUser: user };
      });
  },



fetch() {
    let ref = this.get('firebase');
    let authData = ref.getAuth();

    if (!authData) {
      return Ember.RSVP.Promise.reject(new Error('No Firebase session found'));
    }

    return this._findOrCreateUser(authData)
      .then((user) => {
        return { currentUser: user };
      });
  },

close() {
    this.get('firebase').unauth();
    return Ember.RSVP.Promise.resolve({ currentUser: null });
  },


  _findOrCreateUser(authData) {
    let store = this.get('store');

    return store.find('user', authData.uid)
      .catch(() => {
        let newUser = store.createRecord('user', this.extractUserProperties(authData));

        return newUser.save();
      });
  },

  /**
   * Extract the user properties from `authData` that you care about.
   *
   * @param  {Object} authData
   * @return {Object} An updated property hash
   */
  extractUserProperties(authData) {
    var name = 'Unknown';
    var provider = authData.provider;
    var userData = authData[provider];

    if (userData.displayName) {
      name = userData.displayName;
    } else if (userData.username) {
      name = userData.username;
    }

    return {
      id: authData.uid,
      name: name,
      email: userData.email || null
    };
  }


});

app/routes/application.js

import Ember from 'ember';


export default Ember.Route.extend({
  beforeModel(){
        this.get('session').fetch().catch(function(){});
    },
   actions: {
       logout: function() {
           this.get('session').close().then(function() {
               this.transitionTo('application');
           }.bind(this));
       }
   }
});

app/routes/login.js:

import Ember from 'ember';

export default Ember.Route.extend({

      beforeModel(){
        this.get('session').fetch().catch(function(){});
    },

    actions: {
        login: function() {
          var controller = this.get('controller');
          var email = controller.get('userEmail');
          var password = controller.get('userPassword');
            this.get('session').open('firebase', {
                 provider: 'password',
                 email: email,
                 password: password
            }).then(function() {
                this.transitionTo('dashboard');
            }.bind(this));
        }
      }
});

Thanks to anyone in advance.




Aucun commentaire:

Enregistrer un commentaire