[英]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.