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?
My stack
-------------------------------
Ember : 2.2.0
Ember Data : 2.2.1
jQuery : 1.11.3
Ember Simple Auth : 1.0.0
-------------------------------