我正在尝试编写自定义身份验证器,类似于this example in the docs中的身份验证器。目标是能够通过session.user
检索当前登录的用户。
我正在使用Ember 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 :(得分:5)
从Simple Auth 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);
}
};
答案 1 :(得分: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
我认为可能发生的是您正在使用应用程序注册身份验证器而不是身份验证器本身?
答案 2 :(得分: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
);