Mik*_*keV 0 ember.js ember-cli
我正在尝试验证会话对象在我的ember应用程序中是否可用.我使用ember-cli生成应用程序,我遵循了ember-auth安装说明.说明说"将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;
Run Code Online (Sandbox Code Playgroud)
//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;
}
}
});
Run Code Online (Sandbox Code Playgroud)
小智 5
以下是作者最近的ember-cli-simple-auth设置说明
您不必手动设置初始化程序.我可以验证作者的指示应该在你的控制器中给你this.session.
复制作者的说明:
在ember-cli项目中安装Ember Simple Auth现在非常简单.您所要做的就是从npm安装ember-cli插件:
npm install --save-dev ember-cli-simple-auth
Run Code Online (Sandbox Code Playgroud)
这将把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}}
…
Run Code Online (Sandbox Code Playgroud)
还要在项目的应用程序路径中实现ApplicationRouteMixin:
// app/routes/application.js
import Ember from 'ember';
import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';
export default Ember.Route.extend(ApplicationRouteMixin);
Run Code Online (Sandbox Code Playgroud)
设置身份验证
要实际为用户提供登录选项,我们需要为Ember Simple Auth添加一个身份验证包.假设您运行的是OAuth 2.0兼容服务器http://localhost:3000.要使用它,请安装OAuth 2.0扩展库,这就像从npm安装软件包一样简单:
npm install --save-dev ember-cli-simple-auth-oauth2
Run Code Online (Sandbox Code Playgroud)
与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>
Run Code Online (Sandbox Code Playgroud)
然后在登录控制器中实现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'
});
Run Code Online (Sandbox Code Playgroud)
由于OAuth 2.0身份验证器默认使用相同的域和端口将身份验证请求发送到Ember.js,因此需要将其配置为使用http://localhost:3000:
// config/environment.js
if (environment === 'development') {
…
ENV['simple-auth-oauth2'] = {
serverTokenEndpoint: 'http://localhost:3000/token'
}
…
Run Code Online (Sandbox Code Playgroud)
由于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;
}
};
Run Code Online (Sandbox Code Playgroud)
Ember Simple Auth太棒了!
| 归档时间: |
|
| 查看次数: |
1053 次 |
| 最近记录: |