2014-06-20 442 views
3

我想寫一個自定義身份驗證器,類似於從this example in the docs。目標是能夠通過session.user檢索當前登錄的用戶。自定義身份驗證與Ember簡單身份驗證+ Ember CLI

我用灰燼CLI,所以在initializers/authentication.js

import Ember from 'ember'; 

var customAuthenticator = Ember.SimpleAuth.Authenticators.Devise.extend({ 
    authenticate: function(credentials) { 
    debugger; 
    } 
}); 

export default { 
    name: 'authentication', 

    initialize: function(container, application) { 

    Ember.SimpleAuth.Session.reopen({ 
     user: function() { 
     var userId = this.get('user_id'); 
     if (!Ember.isEmpty(userId)) { 
      return container.lookup('store:main').find('user', userId); 
     } 
     }.property('userId') 
    }); 

    // register the custom authenticator so the session can find it 
    container.register('authenticator:custom', customAuthenticator); 

    Ember.SimpleAuth.setup(container, application, { 
     routeAfterAuthentication: 'landing-pages', 
     authorizerFactory: 'ember-simple-auth-authorizer:devise' 
    }); 
    } 
}; 

當我嘗試驗證,我得到以下錯誤:

TypeError: Cannot read property 'authenticate' of undefined 
at __exports__.default.Ember.ObjectProxy.extend.authenticate 

任何想法,爲什麼?

回答

0

問題是,AMD構建目前沒有自動註冊擴展庫的組件(請參閱https://github.com/simplabs/ember-simple-auth/issues/198)。我將在下一個版本中更改它,並且可能還會採用文檔更專注於AMD構建版本,而不是瀏覽版本。眼下你必須在你的初始化運行這個

container.register(
    'ember-simple-auth-authorizer:devise', 
    Ember.SimpleAuth.Authorizers.Devise 
); 
container.register(
    'ember-simple-auth-authenticator:devise', 
    Ember.SimpleAuth.Authenticators.Devise 
); 
+0

我加這些行,但它仍然不能正常工作 –

+0

在調用'Ember.SimpleAuth.setup'之前你插入了行*嗎? – marcoow

+0

我做到了,儘管我不再需要自定義身份驗證器。 –

1

你需要做這樣的事情:

Em.SimpleAuth.Authenticators.OAuth2.reopen 
    serverTokenEndpoint: "http://myapp.com/token" 
    authenticate: (credentials) -> 
     new Em.RSVP.Promise (resolve, reject) => 
     data = 
      grant_type: "password" 
      username: credentials.identification 
      password: credentials.password 

     @makeRequest(data).then (response) => 
      # success call 
     , (xhr, status, error) -> 
      # fail call 

我想可能發生的是,你是註冊與認證應用程序而不是驗證器本身?

+0

意思是擁有'.extend'而不是'.reopen',儘管它似乎重新打開了包含的驗證器是一個短期解決方案。謝謝! –

5

簡單驗證0.6.4的,你現在可以這樣做:

的index.html:

window.ENV['simple-auth'] = { 
    authorizer: 'simple-auth-authorizer:devise', 
    session: 'session:withCurrentUser' 
}; 

初始化/自定義-session.js:

import Ember from 'ember'; 
import Session from 'simple-auth/session'; 

var SessionWithCurrentUser = Session.extend({ 
    currentUser: function() { 
    var userId = this.get('user_id'); 
    if (!Ember.isEmpty(userId)) { 
     return this.container.lookup('store:main').find('user', userId); 
    } 
    }.property('user_id') 
}); 

export default { 
    name: 'customize-session', 
    initialize: function(container) { 
    container.register('session:withCurrentUser', SessionWithCurrentUser); 
    } 
}; 
+0

這是在最新版本的ember-cli和simple-auth中完成它的方法。但是,將'ENV'綁定到'MyAppENV'可能更明智。因此,在您的index.html:'window.ENV = window.MyAppENV;'(請參閱:http://stackoverflow.com/a/24845613/2637573)。然後在environtment.js中添加ENV:'simple-auth':{authorizer:'simple-auth-authorizer:devise',session:'session:withCurrentUser'}' – AWM

+0

使用ember-cli 0.0.46和simple- auth 0.6.7不需要包含'window.ENV = window.MyAppENV;'或'index.html'中的任何其他內容。 – AWM

相關問題