gpt4 book ai didi

javascript - Ember 2.4 - 让当前用户使用服务并在登录后进行简单的身份验证

转载 作者:行者123 更新时间:2023-12-03 06:48:27 24 4
gpt4 key购买 nike

我在使用实例初始化程序、服务和 ember-simple-auth 库获取当前用户时遇到以下问题。更具体地说,我已经设法获取我的服务“用户”的所有属性,但仅在登录后刷新页面时才获取。我希望我的服务能够在登录后立即访问。我的代码片段如下:

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()”,但我找不到任何合适的解决方案。提前致谢。

最佳答案

我认为当身份验证器解析 promise 时,您应该调用loadCurrentUser()。像这样的东西:

that.get('session').authenticate('authenticator:xxxx',{}).then(() => { 
this.get("account").loadCurrentUser();
});

关于javascript - Ember 2.4 - 让当前用户使用服务并在登录后进行简单的身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37615621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com