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.
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
}
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:
And in the js: