对于这个(也许)愚蠢的问题,我很抱歉。我对这个话题非常陌生!
我创建了一个自定义的授权器:
import Ember from 'ember';
import Base from 'ember-simple-auth/authorizers/base';
export default Base.extend({
authorize: function(jqXHR, requestOptions) {
var accessToken = this.get('session.content.secure.token');
if (this.get('session.isAuthenticated') && !Ember.isEmpty(accessToken)) {
jqXHR.setRequestHeader('Authorization', 'Bearer ' + accessToken);
}
}
});现在我想在我的控制器中的ajax请求中包含令牌(这是我的代码,没有令牌send):
// app/controllers/workouts.js
import Ember from 'ember';
import config from '../config/environment';
export default Ember.Controller.extend({
requestEndpoint: config.ServerIp+'/workouts',
workouts: function() {
Ember.$.ajax({
type: "GET",
url: requestEndpoint
}).success(function(data) {
return data;
})
}.property()
});非常感谢您的帮助和理解这个伟大的模块!
发布于 2015-11-24 22:24:05
你可以有这样的东西。
在您的授权器中:
// app/authorizers/your-authorizer.js
import BaseAuthorizer from 'ember-simple-auth/authorizers/base';
export default BaseAuthorizer.extend({
authorize(data, block) {
const accessToken = data.accessToken; //Data is the response returned by the server
if (!Ember.isEmpty(accessToken)) {
block('Authorization', `Bearer ${accessToken}`);
}
}
});适配器将负责将授权头添加到所有请求:
// app/adapters/application.js
import DS from 'ember-data';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
authorizer: 'authorizer:your-authorizer'
});如果您没有使用ember数据,那么可以看看这个mixin的工作方式来创建您自己的适配器:data-adapter-mixin
要在用户未登录时保护您的路由不被访问,您需要添加经过身份验证的混合:
// app/routes/home.js
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';
export default Route.extend(AuthenticatedRouteMixin, {
...
});别忘了设置一些配置
// config/environment.js
...
var ENV = {
...
'ember-simple-auth': {
authenticationRoute: 'login',
routeAfterAuthentication: 'home',
routeIfAlreadyAuthenticated: 'home'
}
}https://stackoverflow.com/questions/33893079
复制相似问题