solarissf
solarissf

Reputation: 1277

extjs store.load pass authorization header not flowing in

I am trying to pass in the header Authorization: 'Bearer <TOKEN>' via a parameter into my store.load() and it's not working.

It works if I do it like this:

Ext.define('ExtApplication4.model.MenuListModel', {
    extend: 'ExtApplication4.model.Base',
    requires: [
        'ExtApplication4.model.Base'
    ],
    fields: [
        {
            name: 'text',
            type: 'string'
        },
        {
            name: 'iconCls',
            type: 'string'
        },
        {
            name: 'className',
            type: 'string'
        }
    ],
    proxy: {
        type: 'ajax',
        url: 'http://xxxxx/xxx/api/user/getusermenus/',
        reader: {
            type: 'json',
            rootProperty: 'data'
        },
        headers: {
            Authorization: 'Bearer ' + Ext.decode(Ext.util.Cookies.get('token'))
        }
    }
});

The problem is I want to populate the Authorization header in my store.load(). I've spent hours trying to find the syntax to do this. Everything I try the header is not added.

Can someone show me how to do this?

This is what I tried:

targetStore.load({
    params: {
        username: uname
    },
    //headers: {            
    //    Authorization: 'Bearer ' + token;
    //},
    callback: function (records, operation, success) {

Upvotes: 0

Views: 2387

Answers (1)

Alexander
Alexander

Reputation: 20234

You can't do it in your load, just like you can't provide other extraParams in your load. In load, you can only overwrite a small subset of configs, but not nearly all. You will have to set it before you load:

store.getProxy().setHeaders({
    Authorization:'Bearer ' + ...
});
store.load({
    ...

If setHeaders is not a function in your version of ExtJS, you can just set

store.getProxy().headers = {
    Authorization:'Bearer ' + ...
};

Upvotes: 6

Related Questions