Ember 带有自定义服务器身份验证的简单身份验证(未定义凭据)
Ember simple auth with custom server authentication (credentials undefined)
我是 Ember 的新手,正在尝试使用 自定义服务器 [=34] 实现基本身份验证('authentication' header 中的用户名 + 密码) =] 使用 ember 简单身份验证和 ember-cli。问题是 credentials object 在 CustomAuthenticator.
这段代码有什么问题?
app/initializers/login.js
import Ember from 'ember';
import BaseAuthenticator from 'simple-auth/authenticators/base';
import BaseAuthorizer from 'simple-auth/authorizers/base';
window.ENV = window.ENV || {};
window.ENV['simple-auth'] = {
authorizer: 'authorizer:custom',
session: 'session:withCurrentUser'
};
export default {
name: 'authentication',
before: 'simple-auth',
initialize: function(container/*, application*/) {
container.register('authorizer:custom', CustomAuthorizer);
container.register('authenticator:custom', CustomAuthenticator);
}
};
var CustomAuthorizer = BaseAuthorizer.extend({
authorize: function(jqXHR/*, requestOptions*/) {
if (this.get('session.isAuthenticated') && !Ember.isEmpty(this.get('session.token'))) {
jqXHR.setRequestHeader('Authorization', 'Token: ' + this.get('session.token'));
}
}
});
var CustomAuthenticator = BaseAuthenticator.extend({
tokenEndpoint: '/v1/login',
restore: function(data) {
return new Ember.RSVP.Promise(function(resolve, reject) {
if (!Ember.isEmpty(data.token)) {
resolve(data);
} else {
reject();
}
});
},
authenticate: function(credentials) {
//*** HERE THE CREDENTIALS OBJECT IS NULL ***
var _this = this;
if(!Ember.isEmpty(credentials.identification)) {
return this._super(credentials);
} else {
return new Ember.RSVP.Promise(function(resolve, reject) {
Ember.$.ajax({
url: _this.tokenEndpoint,
type: 'POST',
data: JSON.stringify({ session: { identification: credentials.identification, password: credentials.password } }),
contentType: 'application/json'
}).then(function(response) {
Ember.run(function() {
resolve({ token: response.session.token });
});
}, function(xhr/*, status, error*/) {
var response = JSON.parse(xhr.responseText);
Ember.run(function() {
reject(response.error);
});
});
});
}
},
invalidate: function() {
var _this = this;
return new Ember.RSVP.Promise(function(resolve) {
Ember.$.ajax({ url: _this.tokenEndpoint, type: 'DELETE' }).always(function() {
resolve();
});
});
},
});
app/pods/login/controller.js
import AuthenticationControllerMixin from 'simple-auth/mixins/authentication-controller-mixin';
import Ember from 'ember';
export default Ember.Controller.extend(AuthenticationControllerMixin, {
authenticator: 'authenticator:custom'
});
app/pods/login/template.hbs
<div class='container'>
<form {{action 'authenticate' on='submit'}}>
<label for="identification">Login</label>
{{input value=identification placeholder='Enter Login'}}
<label for="password">Password</label>
{{input value=password placeholder='Enter Password' type='password'}}
<button type="submit">Login</button>
</form>
{{#if errorMessage}}
<div class="alert alert-danger">
<p>
<strong>Login failed:</strong> <code>{{errorMessage}}</code>
</p>
</div>
{{/if}}
</div>
import LoginControllerMixin from 'simple-auth/mixins/login-controller-mixin';
而不是
import AuthenticationControllerMixin from 'simple-auth/mixins/authentication-controller-mixin';
我是 Ember 的新手,正在尝试使用 自定义服务器 [=34] 实现基本身份验证('authentication' header 中的用户名 + 密码) =] 使用 ember 简单身份验证和 ember-cli。问题是 credentials object 在 CustomAuthenticator.
这段代码有什么问题?
app/initializers/login.js
import Ember from 'ember';
import BaseAuthenticator from 'simple-auth/authenticators/base';
import BaseAuthorizer from 'simple-auth/authorizers/base';
window.ENV = window.ENV || {};
window.ENV['simple-auth'] = {
authorizer: 'authorizer:custom',
session: 'session:withCurrentUser'
};
export default {
name: 'authentication',
before: 'simple-auth',
initialize: function(container/*, application*/) {
container.register('authorizer:custom', CustomAuthorizer);
container.register('authenticator:custom', CustomAuthenticator);
}
};
var CustomAuthorizer = BaseAuthorizer.extend({
authorize: function(jqXHR/*, requestOptions*/) {
if (this.get('session.isAuthenticated') && !Ember.isEmpty(this.get('session.token'))) {
jqXHR.setRequestHeader('Authorization', 'Token: ' + this.get('session.token'));
}
}
});
var CustomAuthenticator = BaseAuthenticator.extend({
tokenEndpoint: '/v1/login',
restore: function(data) {
return new Ember.RSVP.Promise(function(resolve, reject) {
if (!Ember.isEmpty(data.token)) {
resolve(data);
} else {
reject();
}
});
},
authenticate: function(credentials) {
//*** HERE THE CREDENTIALS OBJECT IS NULL ***
var _this = this;
if(!Ember.isEmpty(credentials.identification)) {
return this._super(credentials);
} else {
return new Ember.RSVP.Promise(function(resolve, reject) {
Ember.$.ajax({
url: _this.tokenEndpoint,
type: 'POST',
data: JSON.stringify({ session: { identification: credentials.identification, password: credentials.password } }),
contentType: 'application/json'
}).then(function(response) {
Ember.run(function() {
resolve({ token: response.session.token });
});
}, function(xhr/*, status, error*/) {
var response = JSON.parse(xhr.responseText);
Ember.run(function() {
reject(response.error);
});
});
});
}
},
invalidate: function() {
var _this = this;
return new Ember.RSVP.Promise(function(resolve) {
Ember.$.ajax({ url: _this.tokenEndpoint, type: 'DELETE' }).always(function() {
resolve();
});
});
},
});
app/pods/login/controller.js
import AuthenticationControllerMixin from 'simple-auth/mixins/authentication-controller-mixin';
import Ember from 'ember';
export default Ember.Controller.extend(AuthenticationControllerMixin, {
authenticator: 'authenticator:custom'
});
app/pods/login/template.hbs
<div class='container'>
<form {{action 'authenticate' on='submit'}}>
<label for="identification">Login</label>
{{input value=identification placeholder='Enter Login'}}
<label for="password">Password</label>
{{input value=password placeholder='Enter Password' type='password'}}
<button type="submit">Login</button>
</form>
{{#if errorMessage}}
<div class="alert alert-danger">
<p>
<strong>Login failed:</strong> <code>{{errorMessage}}</code>
</p>
</div>
{{/if}}
</div>
import LoginControllerMixin from 'simple-auth/mixins/login-controller-mixin';
而不是
import AuthenticationControllerMixin from 'simple-auth/mixins/authentication-controller-mixin';