Search code examples
ember.jsember-simple-auth

Ember CLI - Injecting a service in route and get the current User with ember-simple-auth


I am using ember-simple-auth to authenticate. I have created the service to get the current user.

//services/session.js
import Ember from 'ember';
import ESASession from "ember-simple-auth/services/session";

 export default ESASession.extend({

  store: Ember.inject.service(),

  setCurrentUser: function() {
    if (this.get('isAuthenticated')) {
      let user_id = this.get('session.content.authenticated.user_id');
      this.get('store').findRecord('user', user_id).then((user) => {
        this.set('currentUser', user);
      });
    }
  }.observes('isAuthenticated')
 });

In the controller when I inject the session is working

//controller/application.js
import Ember from 'ember';

export default Ember.Controller.extend({
  auth: Ember.inject.service('session'), 

  actions: {

    save() {
       var current_user = this.get('auth.currentUser');
       console.log(current_user) // I got the current user model
    }
  }
});

But when I try to use the same in the route, my current_user is undefined

//routes/application.js
import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';

export default Ember.Route.extend(ApplicationRouteMixin, {
  auth: Ember.inject.service('session'),

  model() {
    console.log(this.get('auth.currentUser')); // I got undefined
  }
 });

I need to get the current user, because I want to know if the user has the is_admin flag, something like

afterModel: function(model, transition) {
    if (model.is_admin) {
        this.transitionTo('admin');
     }
}

What is the best way to inject the session in routes? What I am doing wrong?


Solution

  • I have to change my session service

    //services/session.js
    import Ember from 'ember';
    import DS from 'ember-data';
    import SessionService from "ember-simple-auth/services/session";
    
    export default SessionService.extend({
    
      store: Ember.inject.service(),
    
      account: Ember.computed('session.content.authenticated.user_id', function() {
        const user_id =  this.get('session.content.authenticated.user_id');
        if (!Ember.isEmpty(user_id)) {
          return DS.PromiseObject.create({
            promise: this.get('store').findRecord('user', user_id)
          });
        }
      })
    });
    

    Now, I can use it in a route; for example, If I want to redirect if the user has first login flag

    //routes/application.js
    import Ember from 'ember';
    import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
    
    export default Ember.Route.extend(ApplicationRouteMixin, {
    
      session: Ember.inject.service(),
    
      sessionAuthenticated() {
        var applicationRouteInstance = this;
        this.get('session.account').then(function(user){
            if (user.get('is_first_login')) {
              applicationRouteInstance.transitionTo('users/password-reset');
            } else {
              applicationRouteInstance.transitionTo('dashboard');
            }
          });
       }
    
    });