с просторов интернета:
// Add a request interceptor
axios.interceptors.request.use(function (config) {
// assume your access token is stored in local storage
// (it should really be somewhere more secure but I digress for simplicity)
let token = localStorage.getItem('access_token')
if (token) {
config.headers['Authorization'] = `Bearer ${token}`
}
return config;
}, function (error) {
// Do something with request error
return Promise.reject(error);
});
и еще:
To give a better answer to this thread, I did the following:
In my layouts app file I added:
<script>
window.App = {!! json_encode([
'apiToken' => Auth::user()->api_token,
]) !!};
</script>
and in my bootstrap.js I added:
axios.defaults.headers.common = {
'X-Requested-With': 'XMLHttpRequest',
'Authorization': 'Bearer ' + App.apiToken,
};
With this I always send the api_token with the logged in user when ever I make a request with Axios. Probably not the best way to go about it, and one should probably use Passport for this.