1

I am using Ember-simple-auth to authenticate my routes, and is working fine. I have:

login - login route
index - after login in ok

And i'm using the ember-cli-notification to pop up this message:

"Welcome USER NAME"

Like this, in IndexController:

export default Ember.Controller.extend({
  init () {
    this.notifications.addNotification({
      message: 'Welcome USER NAME',
      type: 'success',
      autoClear: true,
      clearDuration: 5000
    });
  }
});

I would like to ask... how can i access the properties of the current logged user? I'm in a complete different controller, so i don't have any idea in how can i do that...

And.. i would like to ask if possible.. i am using the init function for this.. am i doing it right?

Thanks.

1 Answers1

0

Use session service. If you return the current user's details in the response, they are persisted in session.data.authenticated.

// app/controllers/index.js
import Ember from 'ember';

export default Ember.Controller.extend({
 session: Ember.inject.service(),
 init () {
   this.notifications.addNotification({
     message: `Welcome ${this.get('session.data.authenticated.name')}`,
     type: 'success',
     autoClear: true,
     clearDuration: 5000
   });
 }
});

Refer Docs & Dummy app

Hope it helps...

selvagsz
  • 3,852
  • 1
  • 24
  • 34
  • about use init to do what... is that the correct way? –  Nov 22 '15 at 05:27
  • to do this** @selvagsz –  Nov 22 '15 at 05:34
  • I'd prefer to have this top level route's `activate` hook. Probably, in `application` route Nevertheless, if you override `init` anywhere, make sure to call `this._super()` https://dockyard.com/blog/2015/10/19/2015-dont-dont-override-init – selvagsz Nov 22 '15 at 05:49
  • More apt hook would be `sessionAuthenticated` of the application route mixin that ember-simple-auth provides http://pastie.org/10573311 http://ember-simple-auth.com/api/classes/ApplicationRouteMixin.html#method_sessionAuthenticated – selvagsz Nov 22 '15 at 05:54