I have the following problem with getting the current user using an instance initializer, a service and ember-simple-auth library. More specifically, I have managed to get all the attributes of my service "user" but only when I refresh the page after login. I would like the promise that my service returns to be immediately accessible right after login. My code snippets are available below:
app/instance-initializers/current-user.js
export function initialize(appInstance) {
appInstance.inject('route', 'account', 'service:account');
appInstance.inject('controller', 'account', 'service:account');
appInstance.inject('template', 'account', 'service:account');
}
export default {
name: 'account',
initialize: initialize
};
app/services/account.js import Ember from 'ember';
export default Ember.Service.extend({
session: Ember.inject.service('session'),
store: Ember.inject.service('store'),
loadCurrentUser() {
const accountId = this.get('session.data.authenticated.auth_token');
console.log("This is my account", accountId);
if (!Ember.isEmpty(accountId)) {
this.get('store').findRecord('profile', accountId).then((profile) => {
this.set('user', profile);
console.log("account_group",this.get('user.user_group'));
console.log("account_username",this.get('user.username'));
});
}
return this.get('user')
}
});
Then I call the loadCurrentUser function in my application route:
import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
export default Ember.Route.extend(ApplicationRouteMixin,{
beforeModel(){
console.log("This is my currentUser", this.get("account").loadCurrentUser());
},
});
And finally in my template I can access the username of my user like this:
Logged in as:
but only when I refresh my page.
I assume that I call my function "loadCurrentUser()" in the wrong place but I can't find anywhere a proper solution. Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire