aqu*_*tae 5 ember.js django-rest-framework ember-simple-auth
我正在尝试使用django-rest-framework后端设置ember-simple-auth,但是我在将用户保存到会话时遇到了一些麻烦.我必须能够在我的模板中做这样的事情:
<h2>Welcome back, {{session.user}}</h2>
Run Code Online (Sandbox Code Playgroud)
因此,根据我发现的几个指南,我已经获得了身份验证和授权,因此我可以获得有效的令牌并在请求中使用.为了让用户参与会话,我进行了修改,App.CustomAuthenticator.authenticate以便在返回令牌时,用户名也会存储到会话中:
authenticate: function(credentials) {
var _this = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
Ember.$.ajax({
url: _this.tokenEndpoint,
type: 'POST',
data: JSON.stringify({username: credentials.identification, password: credentials.password }),
contentType: 'application/json'
}).then(function(response) {
Ember.run(function() {
resolve({
token: response.token,
username: credentials.identification
});
});
}, function(xhr, status, error) {
var response = JSON.parse(xhr.responseText);
Ember.run(function() {
reject(response.error);
});
});
});
},
Run Code Online (Sandbox Code Playgroud)
然后我修改Application.intializer了session一个user属性:
Ember.Application.initializer({
name: 'authentication',
before: 'simple-auth',
initialize: function(container, application) {
// register the custom authenticator and authorizer so Ember Simple Auth can find them
container.register('authenticator:custom', App.CustomAuthenticator);
container.register('authorizer:custom', App.CustomAuthorizer);
SimpleAuth.Session.reopen({
user: function() {
var username = this.get('username');
if (!Ember.isEmpty(username)) {
return container.lookup('store:main').find('user', {username: username});
}
}.property('username')
});
}
});
Run Code Online (Sandbox Code Playgroud)
但是,{{session.user.username}}渲染时它只是一个空字符串.我的问题是:
User对象,所以如何解决它?mic*_*ael 14
要标记@ marcoow的响应,以下是如何在Ember CLI中实现它:
index.html的:
window.ENV['simple-auth'] = {
authorizer: 'simple-auth-authorizer:devise',
session: 'session:withCurrentUser'
};
Run Code Online (Sandbox Code Playgroud)
初始化/自定义-session.js:
import Session from 'simple-auth/session';
var SessionWithCurrentUser = Session.extend({
currentUser: function() {
var userId = this.get('user_id');
if (!Ember.isEmpty(userId)) {
return this.container.lookup('store:main').find('user', userId);
}
}.property('user_id')
});
export default {
name: 'customize-session',
initialize: function(container) {
container.register('session:withCurrentUser', SessionWithCurrentUser);
}
};
Run Code Online (Sandbox Code Playgroud)
在 0.6.4 版本中,您现在可以指定自定义会话类,而无需重新打开,请参阅此处的发行说明:https: //github.com/simplabs/ember-simple-auth/releases/tag/0.6.4。它是这样工作的:
\n\nApp.CustomSession = SimpleAuth.Session.extend({\n account: function() {\n var accountId = this.get(\'account_id\');\n if (!Ember.isEmpty(accountId)) {\n return this.container.lookup(\'store:main\').find(\'account\', accountId);\n }\n }.property(\'account_id\')\n});\n\xe2\x80\xa6\ncontainer.register(\'session:custom\', App.CustomSession);\n\xe2\x80\xa6\nwindow.ENV[\'simple-auth\'] = {\n session: \'session:custom\',\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
5102 次 |
| 最近记录: |