Syntax to get current user attributes in a route hander when using ESA


#1

I’m confused on how to get the current user attributes in a route handler when using the way to manage Current User as explained at ESA wiki page.

Here is my dashboard router:

# routes/dashboard.js
import Route from '@ember/routing/route';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';
import { inject as service } from '@ember/service';
import Ember from 'ember';

export default Route.extend(AuthenticatedRouteMixin, {  
  currentUser: service(),
  currentShop: service(),

  model() {
    let user = this.get('currentUser');
    console.log('++++++ ' + Ember.get(user, 'name'));
    return this.get('store').findAll('shop');
  }
});

The current user/user is set up in current-user.js service:

mport RSVP from 'rsvp';
import Service, { inject as service } from '@ember/service';

export default Service.extend({
  session: service('session'),
  store: service(),
  flashMessages: service('flash-messages'),
  i18n: service('i18n'),

  loadCurrentUser() {
    this.get('flashMessages').clearMessages();
    if (this.get('session.isAuthenticated')) {
      return this.get('store').queryRecord('user', { me: true }).then((user) => {
        this.get('flashMessages').success(this.get('i18n').t('flash.signed_in'));
        this.set('user', user);
      });
    } else {
      this.get('flashMessages').info(this.get('i18n').t('flash.signed_off'));
      return RSVP.resolve();
    }
  }
});

I’m still getting undefined when I try to display it in the console. What am I doing wrong ? An is it possible to extract a shop relation from current User if it has a belongs_to: shop relation in it model:

import DS from 'ember-data';

export default DS.Model.extend({
  username:          DS.attr('string'),
  first_name:        DS.attr('string'),
  last_name:         DS.attr('string'),
  email:             DS.attr('string'),
  shop_identifier:   DS.attr('number'),
  shop:              DS.belongsTo('shop')
});

#2

These two lines in your route seem problematic:

    let user = this.get('currentUser');
    console.log('++++++ ' + Ember.get(user, 'name'));

The first one is getting the service, not the user model ON the service. I think you’d want to change it to:

    let user = this.get('currentUser.user');

So you’re getting “currentUser” (the service) from this (the route) and then getting the user property of that service.

The second line also seems problematic because you’re trying to essentially do user.get('name') which, if the model you posted above is accurate, doesn’t exist on the model (there is username, first_name, and last_name but no name, perhaps it’s a CP that you didn’t include?)


#3

Thank you very much, the below syntax worked, (name attribute was my fault):

let user = this.get('currentUser.user');
console.log("++++ " + user.get('username'));

But if I log off and retry, I get:

Error while processing route: dashboard user is undefined model
TypeError: user is undefined[Learn More]
dashboard.js:13

as if in model hook, the let user = this.get('currentUser.user'); is null.


#4

When I set a service value in a route handler:

# routes/dashboard.js
model() {
    let user = this.get('currentUser.user');
    this.get('currentShop').setShop(user.get('shop'));
    return this.get('store').findAll('shop');
  }

The value of currentShop.shop.name is available only in dashboard.hbs template… Why is that value is no more available in other routes, controllers, templates ? Should create an initializer to achieve that like explained here or there is a better way ?


#5

Ok, I removed the assigning current shop value from models hooks and put it in current-user.js service:

export default Service.extend({
  session: service('session'),
  store: service(),
  flashMessages: service('flash-messages'),
  i18n: service('i18n'),
  currentShop: service('current-shop'),

  loadCurrentUser() {
    this.get('flashMessages').clearMessages();
    if (this.get('session.isAuthenticated')) {
      return this.get('store').queryRecord('user', { me: true }).then((user) => {
        this.get('flashMessages').success(this.get('i18n').t('flash.signed_in'));
        this.get('currentShop').setShop(user.get('shop'));
        this.set('user', user);
      });
    } else {
      this.get('flashMessages').info(this.get('i18n').t('flash.signed_off'));
      return RSVP.resolve();
    }
  }
});

it seems to work as expected for the moment :).