I have an EmberJS application generated using ember-cli. I'm currently using simple-auth with a custom authenticator.
我有一个使用ember-cli生成的EmberJS应用程序。我目前正在使用带有自定义身份验证器的simple-auth。
In the authenticator, when the user logs in I want to save his details so that I can use it later. I have the following code:
在身份验证器中,当用户登录时,我想保存他的详细信息,以便我以后可以使用它。我有以下代码:
authenticate: function(options) {
var self = this;
return new Ember.RSVP.Promise(function(resolve, reject){
API.user.login(options.username, options.password, true).done(function(data) {
// @TODO: Save current user
resolve(data.id);
}).fail(function() {
reject();
});
});
},
User data is available in the variable data.user
.
用户数据在变量data.user中可用。
I tried using Ember.set('App.currentUser', data.user);
but it's not working. What should I do?
我尝试使用Ember.set('App.currentUser',data.user);但它不起作用。我该怎么办?
2 个解决方案
#1
I think it works easiest to use an initializer. Theres several ways you can resolve the user, I think it is easiest if you pass the user_email alongside the grant token from the API
我认为使用初始化程序最简单。有几种方法可以解决用户问题,我认为如果你将API中的user_email与授权令牌一起传递是最简单的
//initializers/session-user.js
import Ember from "ember";
import Session from "simple-auth/session";
export function initialize(container) {
Session.reopen({
setCurrentUser: function() {
var accessToken = this.get('access_token');
var self = this;
if (!Ember.isEmpty(accessToken)) {
return container.lookup('store:main').find('user', {
email: self.get('user_email')
}).then(function (users){
self.set('currentUser', users.get('firstObject'));
});
}
}.observes('access_token')
});
}
export default {
name: 'session-user',
before: 'simple-auth',
initialize: initialize
};
Check this thread for where the idea of this came from: http://discuss.emberjs.com/t/best-practice-for-loading-and-persisting-current-user-in-an-authenticated-system/6987
请查看此主题以了解其中的想法:http://discuss.emberjs.com/t/best-practice-for-loading-and-persisting-current-user-in-an-authenticated-system/6987
And if you are using simple-auth > 0.8.0-beta.1 you will need to adjust the initializer
如果您使用的是simple-auth> 0.8.0-beta.1,则需要调整初始化程序
#2
I ended up creating a custom Sessions
controller and setting the current user object there, and then creating an alias from the application controller.
我最终创建了一个自定义Sessions控制器并在那里设置当前用户对象,然后从应用程序控制器创建一个别名。
Something like what's in this article.
像本文中的内容。
#1
I think it works easiest to use an initializer. Theres several ways you can resolve the user, I think it is easiest if you pass the user_email alongside the grant token from the API
我认为使用初始化程序最简单。有几种方法可以解决用户问题,我认为如果你将API中的user_email与授权令牌一起传递是最简单的
//initializers/session-user.js
import Ember from "ember";
import Session from "simple-auth/session";
export function initialize(container) {
Session.reopen({
setCurrentUser: function() {
var accessToken = this.get('access_token');
var self = this;
if (!Ember.isEmpty(accessToken)) {
return container.lookup('store:main').find('user', {
email: self.get('user_email')
}).then(function (users){
self.set('currentUser', users.get('firstObject'));
});
}
}.observes('access_token')
});
}
export default {
name: 'session-user',
before: 'simple-auth',
initialize: initialize
};
Check this thread for where the idea of this came from: http://discuss.emberjs.com/t/best-practice-for-loading-and-persisting-current-user-in-an-authenticated-system/6987
请查看此主题以了解其中的想法:http://discuss.emberjs.com/t/best-practice-for-loading-and-persisting-current-user-in-an-authenticated-system/6987
And if you are using simple-auth > 0.8.0-beta.1 you will need to adjust the initializer
如果您使用的是simple-auth> 0.8.0-beta.1,则需要调整初始化程序
#2
I ended up creating a custom Sessions
controller and setting the current user object there, and then creating an alias from the application controller.
我最终创建了一个自定义Sessions控制器并在那里设置当前用户对象,然后从应用程序控制器创建一个别名。
Something like what's in this article.
像本文中的内容。