需要帮助,
我对Ember很新,并试图让一个Auth服务在CLI中登录/退出...但是我似乎无法调用任何方便的ORM帮助程序而不会得到未定义的错误......
我确实使用了直接的旧jquery ajax,但我真的想让它与其余的适配器一起工作。
SERVICES / AUTHENTICATION.JS
import Ember from 'ember';
import DS from 'ember-data';
import App from '../app.js';
import User from '../models/user.js';
import ApiKey from '../models/api-key.js';
export default Ember.Object.extend({
//load the current user if the cookies exist and is valid
init: function() {
this._super();
var accessToken = Ember.$.cookie('access_token');
var authUserId = Ember.$.cookie('auth_user');
if (!Ember.isEmpty(accessToken) && !Ember.isEmpty(authUserId)) {
this.authenticate(accessToken, authUserId);
}
},
//determine if the user is currently authenticated
isAuthenticated: function() {
return !Ember.isEmpty(this.get('apiKey.accessToken')) && !Ember.isEmpty(this.get('apiKey.user'));
},
// Auth the user. Once they are auth, set access token to be submitted with all
// future AJAX requests
authenticate: function(accessToken, userId) {
Ember.$.ajaxSetup({
headers: { 'Authorization': 'Bearer ' + accessToken }
});
user = User.find(userId)
this.set('apiKey', ApiKey.create({
accessToken: accessToken,
user: user
}));
},
//log out the user
reset: function({
App.__container__.lookup('route:application').transitionTo('session.new');
Ember.run.sync();
Ember.run.next('apiKey', null);
Ember.$.ajaxSetup({
headers: { 'Authorization' : 'Bearer none' }
});
},
// Ensure that when the browser the cookies are refreshed
apiKeyObserver: function() {
if (Ember.isEmpty(this.get('apiKey'))) {
Ember.$.removeCookie('access_token');
Ember.$.removeCookie('auth_user');
} else {
Ember.$.cookie('access_token', this.get('apiKey.accessToken'));
Ember.$.cookie('auth_user', this.get('apiKey.user.id'));
}
}
});
// // Reset the authentication if any ember data request returns a 401 unauthorized error
DS.rejectionHandler = function(reason) {
if (reason.status === 401) {
App.AuthManager.reset();
}
throw reason;
};