Ember 2.4 - 获取当前用户使用服务和登录后的简单身份验证
Ember 2.4 - get current user using a service and simple auth after login
我在使用实例初始值设定项、服务和 ember-simple-auth 库获取当前用户时遇到以下问题。
更具体地说,我已经设法获得了我的服务 "user" 的所有属性,但仅当我在登录后刷新页面时才获得。
我希望能够在登录后立即访问我的服务 returns。
我的代码片段如下:
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')
}
});
然后我在我的应用程序路由中调用 loadCurrentUser 函数:
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());
},
});
最后在我的模板中,我可以像这样访问我的用户的用户名:
Logged in as: {{account.user.username}}
但仅当我刷新页面时。
我假设我在错误的地方调用了我的函数 "loadCurrentUser()" 但我找不到任何合适的解决方案。
提前致谢。
我认为您应该在身份验证器解决承诺时调用 loadCurrentUser()
。类似于:
that.get('session').authenticate('authenticator:xxxx',{}).then(() => {
this.get("account").loadCurrentUser();
});
我在使用实例初始值设定项、服务和 ember-simple-auth 库获取当前用户时遇到以下问题。 更具体地说,我已经设法获得了我的服务 "user" 的所有属性,但仅当我在登录后刷新页面时才获得。 我希望能够在登录后立即访问我的服务 returns。 我的代码片段如下:
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')
}
});
然后我在我的应用程序路由中调用 loadCurrentUser 函数:
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());
},
});
最后在我的模板中,我可以像这样访问我的用户的用户名:
Logged in as: {{account.user.username}}
但仅当我刷新页面时。
我假设我在错误的地方调用了我的函数 "loadCurrentUser()" 但我找不到任何合适的解决方案。 提前致谢。
我认为您应该在身份验证器解决承诺时调用 loadCurrentUser()
。类似于:
that.get('session').authenticate('authenticator:xxxx',{}).then(() => {
this.get("account").loadCurrentUser();
});