Quantcast
Viewing all articles
Browse latest Browse all 4826

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

@belgoros wrote:

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')
});

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4826

Trending Articles