繁体   English   中英

无法使用ember-cli加载ember-simple-auth

[英]Trouble loading ember-simple-auth with ember-cli

我正在尝试验证该会话对象在我的ember应用程序中是否可用。 我使用ember-cli生成了应用程序,并且遵循了ember-auth安装说明 说明说:“将Ember CLI插件添加到您的项目中,Ember Simple Auth将自行设置”。

npm install --save-dev ember-cli-simple-auth

不幸的是,当我在控制器中时,没有会话对象。

我也尝试将initalizer加载到我的app.js中,但是我还没有弄清楚如何从我的控制器访问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;
        }
    }
});

这是作者最近的ember-cli-simple-auth设置说明

您不必手动设置初始化程序。 而且我可以验证作者的指示应在您的控制器中为您提供this.session。

复制作者的说明

现在,在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非常棒!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM