Ember 简单验证 - 将当前用户注入每个路由
Ember Simple Auth - injecting current user into every route
我正在使用 Ember Simple Auth.
构建网站
我按照 these instructions 尝试将当前用户对象添加到会话中,它成功了,使用了这个稍微修改过的代码:
import Ember from 'ember';
import Session from 'simple-auth/session';
export default {
name: "current-user",
before: "simple-auth",
initialize: function(container) {
Session.reopen({
setCurrentUser: function() {
var accessToken = this.get('secure.token');
var _this = this;
if (!Ember.isEmpty(accessToken)) {
return container.lookup('store:main').find('user', 'me').then(function(user) {
_this.set('content.currentUser', user);
});
}
}.observes('secure.token'),
setAccount: function() {
var _this = this;
return container.lookup('store:main').find('account', this.get('content.currentUser.account.content.id')).then(function(account) {
_this.set('content.account', account);
});
}.observes('content.currentUser'),
});
}
};
但是,使用最新版本的 Ember 我得到以下信息:
DEPRECATION: lookup
was called on a Registry. The initializer
API no longer receives a container, and you should use an instanceInitializer
to look up objects from the container. See http://emberjs.com/guides/deprecations#toc_deprecate-access-to-instances-in-initializers for more details.
我知道我需要将上面的内容拆分为 /app/initializers 和 /app/instance-initializers(根据注释 here),但我不太确定如何去做。
当然,如果有 easier/cleaner 方法可以让每个 route/template 用户和帐户对象可用,我很想听听他们的意见:)
谢谢
这是我前几天做的 initializer/instance-initializer 的前后对比。
之前
export function initialize( container, application ) {
var session = Ember.Object.create({
user:null,
authorization:null
});
application.register('session:main', session, { instantiate: false });
application.inject('route', 'session', 'session:main');
application.inject('controller', 'session', 'session:main');
application.inject('adapter', 'session', 'session:main');
}
之后
export function initialize( instance) {
var session = Ember.Object.create({
user:null,
authorization:null
});
instance.registry.register('session:main', session, { instantiate: false });
instance.registry.injection('route', 'session', 'session:main');
instance.registry.injection('controller', 'session', 'session:main');
instance.registry.injection('adapter', 'session', 'session:main');
}
Ember 数据资料
Ember 应使用 store:application
获取最新迭代中的数据
export function initialize(instance) {
var store = instance.container.lookup('store:application');
....
}
export default {
name: 'socket',
initialize: initialize,
after:['ember-data']
};
首先,您不应重新打开会话,而应改用自定义会话(参见此示例:https://github.com/simplabs/ember-simple-auth/blob/master/examples/4-authenticated-account.html#L132)。此外,您不仅应该在设置访问令牌时加载当前用户,还应该在会话经过身份验证('session.get('isAuthenticated')'
)时加载当前用户,这使得您的代码不依赖于身份验证器。
有关在初始化程序中使用注册表的弃用警告有望在 ESA 0.9.0 中消失。
这对我有用:
- ember-cli: 0.2.7 (ember: 1.12.0, ember-数据: 1.0.0-beta.18)
- ember-cli-simple-auth: 0.8.0-beta.3
注:
- ember-数据:1.13。 Store 已在初始化程序中注册,应该按原样工作
- ember-数据:1.0.0-beta.19。 Store 已在实例初始化程序中注册,需要进行一些调整
1) 自定义会话
//config/environment.js
ENV['simple-auth'] = {
session: 'session:custom',
...
}
//app/sessions/custom.js
import Session from 'simple-auth/session';
export default Session.extend({
// here _store is ember-data store injected by initializer
// why "_store"? because "store" is already used by simple-auth as localStorage
// why initializer? I tried
// _store: Ember.inject.service('store') and got error
currentUser: function() {
var userId = this.get('secure.userId');
if (userId && this.get('isAuthenticated')) {
return this._store.find('user', userId);
}
}.property('secure.userId', 'isAuthenticated')
});
2) 通过初始化程序将存储注入会话(否则 find() 将无法工作)
//app/initializers/session-store
export function initialize(container, application) {
application.inject('session:custom', '_store', 'store:main')
// "store:main" is highly dynamic depepeding on ember-data version
// in 1.0.0-beta.19 (June 5, 2015) => "store:application"
// in 1.13 (June 16, 2015) => "service:store"
}
export default {
name: 'session-store',
after: 'ember-data',
initialize: initialize
}
3) 在模板中
{{#if session.isAuthenticated}}
{{session.currentUser.name}}
{{/if}}
注意:这并不能使您免于 ember-simple-auth
本身产生的弃用。
我正在使用 Ember Simple Auth.
构建网站我按照 these instructions 尝试将当前用户对象添加到会话中,它成功了,使用了这个稍微修改过的代码:
import Ember from 'ember';
import Session from 'simple-auth/session';
export default {
name: "current-user",
before: "simple-auth",
initialize: function(container) {
Session.reopen({
setCurrentUser: function() {
var accessToken = this.get('secure.token');
var _this = this;
if (!Ember.isEmpty(accessToken)) {
return container.lookup('store:main').find('user', 'me').then(function(user) {
_this.set('content.currentUser', user);
});
}
}.observes('secure.token'),
setAccount: function() {
var _this = this;
return container.lookup('store:main').find('account', this.get('content.currentUser.account.content.id')).then(function(account) {
_this.set('content.account', account);
});
}.observes('content.currentUser'),
});
}
};
但是,使用最新版本的 Ember 我得到以下信息:
DEPRECATION:
lookup
was called on a Registry. Theinitializer
API no longer receives a container, and you should use aninstanceInitializer
to look up objects from the container. See http://emberjs.com/guides/deprecations#toc_deprecate-access-to-instances-in-initializers for more details.
我知道我需要将上面的内容拆分为 /app/initializers 和 /app/instance-initializers(根据注释 here),但我不太确定如何去做。
当然,如果有 easier/cleaner 方法可以让每个 route/template 用户和帐户对象可用,我很想听听他们的意见:)
谢谢
这是我前几天做的 initializer/instance-initializer 的前后对比。
之前
export function initialize( container, application ) {
var session = Ember.Object.create({
user:null,
authorization:null
});
application.register('session:main', session, { instantiate: false });
application.inject('route', 'session', 'session:main');
application.inject('controller', 'session', 'session:main');
application.inject('adapter', 'session', 'session:main');
}
之后
export function initialize( instance) {
var session = Ember.Object.create({
user:null,
authorization:null
});
instance.registry.register('session:main', session, { instantiate: false });
instance.registry.injection('route', 'session', 'session:main');
instance.registry.injection('controller', 'session', 'session:main');
instance.registry.injection('adapter', 'session', 'session:main');
}
Ember 数据资料
Ember 应使用 store:application
export function initialize(instance) {
var store = instance.container.lookup('store:application');
....
}
export default {
name: 'socket',
initialize: initialize,
after:['ember-data']
};
首先,您不应重新打开会话,而应改用自定义会话(参见此示例:https://github.com/simplabs/ember-simple-auth/blob/master/examples/4-authenticated-account.html#L132)。此外,您不仅应该在设置访问令牌时加载当前用户,还应该在会话经过身份验证('session.get('isAuthenticated')'
)时加载当前用户,这使得您的代码不依赖于身份验证器。
有关在初始化程序中使用注册表的弃用警告有望在 ESA 0.9.0 中消失。
这对我有用:
- ember-cli: 0.2.7 (ember: 1.12.0, ember-数据: 1.0.0-beta.18)
- ember-cli-simple-auth: 0.8.0-beta.3
注:
- ember-数据:1.13。 Store 已在初始化程序中注册,应该按原样工作
- ember-数据:1.0.0-beta.19。 Store 已在实例初始化程序中注册,需要进行一些调整
1) 自定义会话
//config/environment.js
ENV['simple-auth'] = {
session: 'session:custom',
...
}
//app/sessions/custom.js
import Session from 'simple-auth/session';
export default Session.extend({
// here _store is ember-data store injected by initializer
// why "_store"? because "store" is already used by simple-auth as localStorage
// why initializer? I tried
// _store: Ember.inject.service('store') and got error
currentUser: function() {
var userId = this.get('secure.userId');
if (userId && this.get('isAuthenticated')) {
return this._store.find('user', userId);
}
}.property('secure.userId', 'isAuthenticated')
});
2) 通过初始化程序将存储注入会话(否则 find() 将无法工作)
//app/initializers/session-store
export function initialize(container, application) {
application.inject('session:custom', '_store', 'store:main')
// "store:main" is highly dynamic depepeding on ember-data version
// in 1.0.0-beta.19 (June 5, 2015) => "store:application"
// in 1.13 (June 16, 2015) => "service:store"
}
export default {
name: 'session-store',
after: 'ember-data',
initialize: initialize
}
3) 在模板中
{{#if session.isAuthenticated}}
{{session.currentUser.name}}
{{/if}}
注意:这并不能使您免于 ember-simple-auth
本身产生的弃用。