Laravel - Vue JS : What is the best way access backend

Viewed 100

I'm using Laravel API passport for my SPA Authentication using vue. So far what I made is each time I need to access my backend side I need to call header in able to accept by my protected route

const userObj = JSON.parse(window.localStorage.getItem('token'));
var header = {
    'Accept' : 'application/json',
    'Authorization' : 'Bearer '+ userObj.access_token
}
axios.get('/prod/test',{headers : header})
    .then(response=>{
        console.log(response);

    });
}

Is there any much cleaner way to do this thanks

1 Answers

You can set default values to axios: https://github.com/axios/axios#global-axios-defaults

axios.defaults.headers.common['Accept'] = 'application/json'
axios.defaults.headers.common['Authorization'] = 'Bearer '+ userObj.access_token;

In Laravel, you already have the file bootstrap.js which contain some pre-configuration that you can edit.

Related