0

I am trying to add the custom session variable session.currentUser.isBusiness in ember-fire. However, on page reload, the data does not persist and disappears.

I'm putting the login logic within my login route, which looks like this:

import Ember from ‘ember’;

export default Ember.Component.extend({
 classNames: [‘login-form’],
    router: Ember.inject.service(‘-routing’),
   actions: {
   signIn(provider) {
     let controller = this;
     this.get(‘session’).open(‘firebase’, {
       provider: provider,
       email: this.get(‘email’) || ‘’,
       password: this.get(‘password’) || ‘’,
     }).then(() => {
       this.get(‘session’).set(‘currentUser.isBusiness’, true );

       controller.get(‘router’).transitionTo(‘business.portal’);
     }, (error) => {
       alert(error);
     });
   },
   googleSignIn(){
     let controller=this;
           this.get(‘session’).open(‘firebase’, { provider: ‘google’}).then(function(data) {
             data = null;
           controller.transitionToRoute(‘portal’);
           }, (error) => {
       alert(error);
     });
   }
 }
});

It works when I first sign in and I can see that session.currentUser.isBusiness is set to false; however, when the page is reloaded, session.currentUser.isBusiness no longer exists.

I'm using Ember 2.11 and the latest version of ember-fire.

What should I change to make sure the data persists?

beckah
  • 1,543
  • 6
  • 28
  • 61

1 Answers1

0

You are trying to store data into Firebase internal table(actually you are just setting property to session object) and it is not possible. Check this answer.

You can create User model instead:

// app/models/user.js

import DS from 'ember-data';

export default DS.Model.extend({
  // …
  isBusiness: DS.attr('boolean')
});

And check if you already have this user signed in or create new and set all properties if user with this uid was not found.

// component
// …
this
  .get('session')
  .open('firebase', { provider: 'google' })
  .then(() => {
    let uid = this.get('session.currentUser.uid');
    this
      .get('store')
      .findRecord('user', uid)
      .then((user) => {
        if (user) {
          return console.log(user.get('isBusiness')); // => true
        }

        this
          .get('store')
          .createRecord('user', { id: uid, isBusiness: true })
          .save()
          .then((newUser) => {
            console.log(newUser.get('isBusiness')); // => true
          });
      }); 
  });

Code is "abstract" so you can get idea what I mean. Please report any issues if you will find them.

Community
  • 1
  • 1
kolybasov
  • 76
  • 4