Vuetable-2: Authorization header

Created on 10 May 2018  路  3Comments  路  Source: ratiw/vuetable-2

How can I add a token or something else to header before call vuetable call to sever?

I tried to import and customize axios in main.js

axios.interceptors.request.use((request) => { if (request.data && request.headers['Content-Type'] === 'application/x-www-form-urlencoded') { request.data = qs.stringify(request.data) } request.headers['Authorization'] = 'Bearer: ' + store.state.user.main.token return request })
Works for my custom requests on another component, but when its called from vuetable doesn't

My requests
image

vuetable requests

image

Most helpful comment

first create a global instance of axios to use it in your project :

$http = axios.create({
    baseURL: API_BASE_URL,
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + store.state.user.main.token
    }
});

then use the configs of the same instance ($http) in your vuetable :

<vuetable ref="vuetable" :http-options="httpOptions"></vuetable>

<script>
    export default {
        data() {
            return {
                httpOptions: {
                    baseURL: $http.defaults.baseURL,
                    headers: {
                        'Accept': $http.defaults.headers['Accept'],
                        'Content-Type': $http.defaults.headers['Content-Type'],
                        'Authorization': $http.defaults.headers['Authorization']
                    }
                }
            };
        }
    }
</script>

All 3 comments

Please try searching old issues about this first.

first create a global instance of axios to use it in your project :

$http = axios.create({
    baseURL: API_BASE_URL,
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + store.state.user.main.token
    }
});

then use the configs of the same instance ($http) in your vuetable :

<vuetable ref="vuetable" :http-options="httpOptions"></vuetable>

<script>
    export default {
        data() {
            return {
                httpOptions: {
                    baseURL: $http.defaults.baseURL,
                    headers: {
                        'Accept': $http.defaults.headers['Accept'],
                        'Content-Type': $http.defaults.headers['Content-Type'],
                        'Authorization': $http.defaults.headers['Authorization']
                    }
                }
            };
        }
    }
</script>

I had same issue, but in my application the token may change. based on @e-abdelbasset idea, my solution is to use reference types instead of value types:

<vuetable ref="vuetable" :http-options="httpOptions"></vuetable>

<script>
    export default {
        data() {
            return {
                httpOptions: {
                    baseURL: $http.defaults.baseURL,
                    headers: $http.defaults.headers.common
                }
            };
        }
    }
</script>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

wa05 picture wa05  路  3Comments

larryu picture larryu  路  6Comments

jdriesen picture jdriesen  路  4Comments

chrislandeza picture chrislandeza  路  3Comments

legreco picture legreco  路  5Comments