简体   繁体   English

Ember-Simple-Auth:如何在ajax请求中注入令牌

[英]Ember-Simple-Auth: How to inject token in ajax request

sorry for that (maybe) silly question. 对于这个(也许)愚蠢的问题感到抱歉。 Im super new to this topic! 我是这个主题的新手!

I created a custom authorizer: 我创建了一个自定义授权者:

    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);
            }
        }
    });

And now i want to include the token in a ajax request in my controller (this is my code without the token send): 现在我想在控制器中的ajax请求中包括令牌(这是我的代码,没有令牌发送):

// 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()
});

Thank you very much for helping and understanding this great module! 非常感谢您帮助和理解这个很棒的模块!

You could have something like this. 你可能会有这样的事情。

In your authorizer: 在您的授权者中:

// 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}`);
        }
    }
});

The adapter will take care of adding the authorization header to all your requests: 适配器将负责将授权标头添加到您的所有请求中:

// 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'
});

If you are not using ember data, you can take a look the way this mixin works to create your own adapter: data-adapter-mixin 如果您不使用余烬数据,则可以看看此mixin创建自己的适配器的方式: data-adapter-mixin

To protect your route from access if the users are not logged, you need to add the authenticated mixin: 如果用户未登录,则为了防止访问该路由,您需要添加经过身份验证的混合:

// app/routes/home.js
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';

export default Route.extend(AuthenticatedRouteMixin, {
    ...
});

And don't forget to set some configuration 并且不要忘记设置一些配置

// config/environment.js
...
var ENV = {
    ...
    'ember-simple-auth': {
        authenticationRoute: 'login',
        routeAfterAuthentication: 'home',
        routeIfAlreadyAuthenticated: 'home'
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM