Search code examples
javascriptember.jsember-simple-auth

How access session username after session is authenticated in other controller


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.


Solution

  • 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...