Vuetable-2: Attaching bearer token in every datatable api mode request

Created on 19 Jan 2018  路  3Comments  路  Source: ratiw/vuetable-2

How do i attach a bearer token to each http request sent by the datatable in the api mode.
The token is saved in the localstorage as io would also want to listen for cases of 401 and redirrect to login page

I have set up vue resource already like
Vue.use(VueResource);
Vue.http.options.root = appdetails.apiurl
Vue.http.interceptors.push(function (request, next) {
request.headers.set('Authorization', Bearer+tokenservice.gettoken(tokenservice.accesstoken));
next()
});

Now in my vuetable

     <vue-table ref="vuetable"
                       :api-url="apiurl" 

     />
       apiurl:appdetails.apiurl+"/users"

Now the above fails whenenever the route is protected by auth api in laravel.

How do i go about this.

Most helpful comment

If you are placing a GET request to your API, the token can be included in the httpOptions, like:

In the vue template:

<vuetable :http-options="httpOptions" ...

And in the js:

export default {
    data() {
        return {
            httpOptions: { headers: { Authorization: 'Bearer ' + TOKEN } },
            ...

All 3 comments

If you are placing a GET request to your API, the token can be included in the httpOptions, like:

In the vue template:

<vuetable :http-options="httpOptions" ...

And in the js:

export default {
    data() {
        return {
            httpOptions: { headers: { Authorization: 'Bearer ' + TOKEN } },
            ...

@mondul tried this but it doesn't work

@sherlyseptiani try you use this in main.js

axios.defaults.baseURL = process.env.API_LOCATION || 'http://localhost'
if (localStorage.getItem('auth') != null) {
  let auth = JSON.parse(localStorage.getItem('auth'))
  axios.defaults.headers.common['Authorization'] = 'Bearer ' + auth.Token
}

and see this code for use your api

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mannyyang picture mannyyang  路  3Comments

mondul picture mondul  路  3Comments

larryu picture larryu  路  6Comments

kawamataryo picture kawamataryo  路  3Comments

hjJunior picture hjJunior  路  3Comments