我正在尝试验证会话对象在我的ember应用中是否可用。我已经使用ember-cli生成应用程序,并且我已经遵循了ember-auth安装instructions。说明"将Ember CLI Addon添加到您的项目中,Ember Simple Auth将自行设置"。
npm install --save-dev ember-cli-simple-auth
不幸的是,当我在我的控制器中时,没有会话对象。
我也尝试在我的app.js中加载initalizer,但我还没弄明白如何从我的控制器访问App.session。我认为ember-cli的命名空间不同。
//app.js
import Ember from 'ember';
import Resolver from 'ember/resolver';
import loadInitializers from 'ember/load-initializers';
Ember.MODEL_FACTORY_INJECTIONS = true;
var App = Ember.Application.extend({
modulePrefix: 'ember-test', // TODO: loaded via config
Resolver: Resolver
});
loadInitializers(App, 'ember-test');
loadInitializers(App, 'simple-auth');
export default App;
//about.js
import Ember from 'ember';
export default Ember.Controller.extend({
derp: 'derpvalue',
actions: {
test : function(){
console.log("In test");
console.log(session);
console.log(App.session);
debugger;
}
}
});
答案 0 :(得分:5)
以下是recent ember-cli-simple-auth setup instructions from the author
您不必手动设置初始值设定项。我可以验证作者的指示应该在你的控制器中给你这个激情。
在ember-cli项目中安装Ember Simple Auth现在非常简单。您所要做的就是从npm安装ember-cli插件:
npm install --save-dev ember-cli-simple-auth
这会将Ember Simple Auth的AMD发行版安装到项目中,注册初始化程序,以便Ember Simple Auth自动设置自己并将其自身添加为项目的package.json的依赖项。
您可以添加登录路由和登录/注销链接以验证它是否真正有效:
// app/router.js
…
Router.map(function() {
this.route('login');
});
…
// app/templates/application.hbs
…
{{#if session.isAuthenticated}}
<a {{ action 'invalidateSession' }}>Logout</a>
{{else}}
{{#link-to 'login'}}Login{{/link-to}}
{{/if}}
…
还在项目的应用程序路径中实现ApplicationRouteMixin:
// app/routes/application.js
import Ember from 'ember';
import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';
export default Ember.Route.extend(ApplicationRouteMixin);
设置身份验证
要实际为用户提供登录选项,我们需要为Ember Simple Auth添加一个身份验证包。我们假设您有一个在http://localhost:3000
运行的OAuth 2.0兼容服务器。要使用它,请安装OAuth 2.0扩展库,这就像从npm:
npm install --save-dev ember-cli-simple-auth-oauth2
与ember-cli-simple-auth软件包一样,这将自行设置,以便在使用OAuth 2.0功能时无需其他任何操作。
OAuth 2.0身份验证机制需要一个登录表单,所以让我们创建:
// app/templates/login.hbs
<form {{action 'authenticate' on='submit'}}>
<label for="identification">Login</label>
{{input id='identification' placeholder='Enter Login' value=identification}}
<label for="password">Password</label>
{{input id='password' placeholder='Enter Password' type='password' value=password}}
<button type="submit">Login</button>
</form>
然后在登录控制器中实现LoginControllerMixin并使其使用OAuth 2.0身份验证器来执行实际身份验证:
// app/controllers/login.js
import Ember from 'ember';
import LoginControllerMixin from 'simple-auth/mixins/login-controller-mixin';
export default Ember.Controller.extend(LoginControllerMixin, {
authenticator: 'simple-auth-authenticator:oauth2-password-grant'
});
由于OAuth 2.0身份验证器默认使用相同的域和端口将身份验证请求发送到Ember.js,因此需要将其配置为使用http://localhost:3000
代替:
// config/environment.js
if (environment === 'development') {
…
ENV['simple-auth-oauth2'] = {
serverTokenEndpoint: 'http://localhost:3000/token'
}
…
由于ember-cli将所有配置添加到Ember Simple Auth无法使用的全局ENV变量(例如,在本例中为MyAuthAppENV),因为它不知道它的名称,您需要将其复制到window.ENV以便Ember Simple Auth可以使用它:
// app/initializers/simple-auth-config.js
export default {
name: 'simple-auth-config',
before: 'simple-auth',
initialize: function() {
window.ENV = MyAuthAppENV;
}
};
Ember Simple Auth太棒了!